dcdbas.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * dcdbas.c: Dell Systems Management Base Driver
  3. *
  4. * The Dell Systems Management Base Driver provides a sysfs interface for
  5. * systems management software to perform System Management Interrupts (SMIs)
  6. * and Host Control Actions (power cycle or power off after OS shutdown) on
  7. * Dell systems.
  8. *
  9. * See Documentation/dcdbas.txt for more information.
  10. *
  11. * Copyright (C) 1995-2006 Dell Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License v2.0 as published by
  15. * the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/platform_device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/errno.h>
  25. #include <linux/cpu.h>
  26. #include <linux/gfp.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/mc146818rtc.h>
  30. #include <linux/module.h>
  31. #include <linux/reboot.h>
  32. #include <linux/sched.h>
  33. #include <linux/smp.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/string.h>
  36. #include <linux/types.h>
  37. #include <linux/mutex.h>
  38. #include <asm/io.h>
  39. #include "dcdbas.h"
  40. #define DRIVER_NAME "dcdbas"
  41. #define DRIVER_VERSION "5.6.0-3.2"
  42. #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
  43. static struct platform_device *dcdbas_pdev;
  44. static u8 *smi_data_buf;
  45. static dma_addr_t smi_data_buf_handle;
  46. static unsigned long smi_data_buf_size;
  47. static u32 smi_data_buf_phys_addr;
  48. static DEFINE_MUTEX(smi_data_lock);
  49. static unsigned int host_control_action;
  50. static unsigned int host_control_smi_type;
  51. static unsigned int host_control_on_shutdown;
  52. /**
  53. * smi_data_buf_free: free SMI data buffer
  54. */
  55. static void smi_data_buf_free(void)
  56. {
  57. if (!smi_data_buf)
  58. return;
  59. dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
  60. __func__, smi_data_buf_phys_addr, smi_data_buf_size);
  61. dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf,
  62. smi_data_buf_handle);
  63. smi_data_buf = NULL;
  64. smi_data_buf_handle = 0;
  65. smi_data_buf_phys_addr = 0;
  66. smi_data_buf_size = 0;
  67. }
  68. /**
  69. * smi_data_buf_realloc: grow SMI data buffer if needed
  70. */
  71. static int smi_data_buf_realloc(unsigned long size)
  72. {
  73. void *buf;
  74. dma_addr_t handle;
  75. if (smi_data_buf_size >= size)
  76. return 0;
  77. if (size > MAX_SMI_DATA_BUF_SIZE)
  78. return -EINVAL;
  79. /* new buffer is needed */
  80. buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
  81. if (!buf) {
  82. dev_dbg(&dcdbas_pdev->dev,
  83. "%s: failed to allocate memory size %lu\n",
  84. __func__, size);
  85. return -ENOMEM;
  86. }
  87. /* memory zeroed by dma_alloc_coherent */
  88. if (smi_data_buf)
  89. memcpy(buf, smi_data_buf, smi_data_buf_size);
  90. /* free any existing buffer */
  91. smi_data_buf_free();
  92. /* set up new buffer for use */
  93. smi_data_buf = buf;
  94. smi_data_buf_handle = handle;
  95. smi_data_buf_phys_addr = (u32) virt_to_phys(buf);
  96. smi_data_buf_size = size;
  97. dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
  98. __func__, smi_data_buf_phys_addr, smi_data_buf_size);
  99. return 0;
  100. }
  101. static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
  102. struct device_attribute *attr,
  103. char *buf)
  104. {
  105. return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
  106. }
  107. static ssize_t smi_data_buf_size_show(struct device *dev,
  108. struct device_attribute *attr,
  109. char *buf)
  110. {
  111. return sprintf(buf, "%lu\n", smi_data_buf_size);
  112. }
  113. static ssize_t smi_data_buf_size_store(struct device *dev,
  114. struct device_attribute *attr,
  115. const char *buf, size_t count)
  116. {
  117. unsigned long buf_size;
  118. ssize_t ret;
  119. buf_size = simple_strtoul(buf, NULL, 10);
  120. /* make sure SMI data buffer is at least buf_size */
  121. mutex_lock(&smi_data_lock);
  122. ret = smi_data_buf_realloc(buf_size);
  123. mutex_unlock(&smi_data_lock);
  124. if (ret)
  125. return ret;
  126. return count;
  127. }
  128. static ssize_t smi_data_read(struct file *filp, struct kobject *kobj,
  129. struct bin_attribute *bin_attr,
  130. char *buf, loff_t pos, size_t count)
  131. {
  132. ssize_t ret;
  133. mutex_lock(&smi_data_lock);
  134. ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
  135. smi_data_buf_size);
  136. mutex_unlock(&smi_data_lock);
  137. return ret;
  138. }
  139. static ssize_t smi_data_write(struct file *filp, struct kobject *kobj,
  140. struct bin_attribute *bin_attr,
  141. char *buf, loff_t pos, size_t count)
  142. {
  143. ssize_t ret;
  144. if ((pos + count) > MAX_SMI_DATA_BUF_SIZE)
  145. return -EINVAL;
  146. mutex_lock(&smi_data_lock);
  147. ret = smi_data_buf_realloc(pos + count);
  148. if (ret)
  149. goto out;
  150. memcpy(smi_data_buf + pos, buf, count);
  151. ret = count;
  152. out:
  153. mutex_unlock(&smi_data_lock);
  154. return ret;
  155. }
  156. static ssize_t host_control_action_show(struct device *dev,
  157. struct device_attribute *attr,
  158. char *buf)
  159. {
  160. return sprintf(buf, "%u\n", host_control_action);
  161. }
  162. static ssize_t host_control_action_store(struct device *dev,
  163. struct device_attribute *attr,
  164. const char *buf, size_t count)
  165. {
  166. ssize_t ret;
  167. /* make sure buffer is available for host control command */
  168. mutex_lock(&smi_data_lock);
  169. ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
  170. mutex_unlock(&smi_data_lock);
  171. if (ret)
  172. return ret;
  173. host_control_action = simple_strtoul(buf, NULL, 10);
  174. return count;
  175. }
  176. static ssize_t host_control_smi_type_show(struct device *dev,
  177. struct device_attribute *attr,
  178. char *buf)
  179. {
  180. return sprintf(buf, "%u\n", host_control_smi_type);
  181. }
  182. static ssize_t host_control_smi_type_store(struct device *dev,
  183. struct device_attribute *attr,
  184. const char *buf, size_t count)
  185. {
  186. host_control_smi_type = simple_strtoul(buf, NULL, 10);
  187. return count;
  188. }
  189. static ssize_t host_control_on_shutdown_show(struct device *dev,
  190. struct device_attribute *attr,
  191. char *buf)
  192. {
  193. return sprintf(buf, "%u\n", host_control_on_shutdown);
  194. }
  195. static ssize_t host_control_on_shutdown_store(struct device *dev,
  196. struct device_attribute *attr,
  197. const char *buf, size_t count)
  198. {
  199. host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
  200. return count;
  201. }
  202. static int raise_smi(void *par)
  203. {
  204. struct smi_cmd *smi_cmd = par;
  205. if (smp_processor_id() != 0) {
  206. dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
  207. __func__);
  208. return -EBUSY;
  209. }
  210. /* generate SMI */
  211. /* inb to force posted write through and make SMI happen now */
  212. asm volatile (
  213. "outb %b0,%w1\n"
  214. "inb %w1"
  215. : /* no output args */
  216. : "a" (smi_cmd->command_code),
  217. "d" (smi_cmd->command_address),
  218. "b" (smi_cmd->ebx),
  219. "c" (smi_cmd->ecx)
  220. : "memory"
  221. );
  222. return 0;
  223. }
  224. /**
  225. * dcdbas_smi_request: generate SMI request
  226. *
  227. * Called with smi_data_lock.
  228. */
  229. int dcdbas_smi_request(struct smi_cmd *smi_cmd)
  230. {
  231. int ret;
  232. if (smi_cmd->magic != SMI_CMD_MAGIC) {
  233. dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
  234. __func__);
  235. return -EBADR;
  236. }
  237. /* SMI requires CPU 0 */
  238. get_online_cpus();
  239. ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
  240. put_online_cpus();
  241. return ret;
  242. }
  243. /**
  244. * smi_request_store:
  245. *
  246. * The valid values are:
  247. * 0: zero SMI data buffer
  248. * 1: generate calling interface SMI
  249. * 2: generate raw SMI
  250. *
  251. * User application writes smi_cmd to smi_data before telling driver
  252. * to generate SMI.
  253. */
  254. static ssize_t smi_request_store(struct device *dev,
  255. struct device_attribute *attr,
  256. const char *buf, size_t count)
  257. {
  258. struct smi_cmd *smi_cmd;
  259. unsigned long val = simple_strtoul(buf, NULL, 10);
  260. ssize_t ret;
  261. mutex_lock(&smi_data_lock);
  262. if (smi_data_buf_size < sizeof(struct smi_cmd)) {
  263. ret = -ENODEV;
  264. goto out;
  265. }
  266. smi_cmd = (struct smi_cmd *)smi_data_buf;
  267. switch (val) {
  268. case 2:
  269. /* Raw SMI */
  270. ret = dcdbas_smi_request(smi_cmd);
  271. if (!ret)
  272. ret = count;
  273. break;
  274. case 1:
  275. /* Calling Interface SMI */
  276. smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
  277. ret = dcdbas_smi_request(smi_cmd);
  278. if (!ret)
  279. ret = count;
  280. break;
  281. case 0:
  282. memset(smi_data_buf, 0, smi_data_buf_size);
  283. ret = count;
  284. break;
  285. default:
  286. ret = -EINVAL;
  287. break;
  288. }
  289. out:
  290. mutex_unlock(&smi_data_lock);
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(dcdbas_smi_request);
  294. /**
  295. * host_control_smi: generate host control SMI
  296. *
  297. * Caller must set up the host control command in smi_data_buf.
  298. */
  299. static int host_control_smi(void)
  300. {
  301. struct apm_cmd *apm_cmd;
  302. u8 *data;
  303. unsigned long flags;
  304. u32 num_ticks;
  305. s8 cmd_status;
  306. u8 index;
  307. apm_cmd = (struct apm_cmd *)smi_data_buf;
  308. apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
  309. switch (host_control_smi_type) {
  310. case HC_SMITYPE_TYPE1:
  311. spin_lock_irqsave(&rtc_lock, flags);
  312. /* write SMI data buffer physical address */
  313. data = (u8 *)&smi_data_buf_phys_addr;
  314. for (index = PE1300_CMOS_CMD_STRUCT_PTR;
  315. index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
  316. index++, data++) {
  317. outb(index,
  318. (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
  319. outb(*data,
  320. (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
  321. }
  322. /* first set status to -1 as called by spec */
  323. cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
  324. outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
  325. /* generate SMM call */
  326. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  327. spin_unlock_irqrestore(&rtc_lock, flags);
  328. /* wait a few to see if it executed */
  329. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  330. while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
  331. == ESM_STATUS_CMD_UNSUCCESSFUL) {
  332. num_ticks--;
  333. if (num_ticks == EXPIRED_TIMER)
  334. return -ETIME;
  335. }
  336. break;
  337. case HC_SMITYPE_TYPE2:
  338. case HC_SMITYPE_TYPE3:
  339. spin_lock_irqsave(&rtc_lock, flags);
  340. /* write SMI data buffer physical address */
  341. data = (u8 *)&smi_data_buf_phys_addr;
  342. for (index = PE1400_CMOS_CMD_STRUCT_PTR;
  343. index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
  344. index++, data++) {
  345. outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
  346. outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
  347. }
  348. /* generate SMM call */
  349. if (host_control_smi_type == HC_SMITYPE_TYPE3)
  350. outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
  351. else
  352. outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
  353. /* restore RTC index pointer since it was written to above */
  354. CMOS_READ(RTC_REG_C);
  355. spin_unlock_irqrestore(&rtc_lock, flags);
  356. /* read control port back to serialize write */
  357. cmd_status = inb(PE1400_APM_CONTROL_PORT);
  358. /* wait a few to see if it executed */
  359. num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
  360. while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
  361. num_ticks--;
  362. if (num_ticks == EXPIRED_TIMER)
  363. return -ETIME;
  364. }
  365. break;
  366. default:
  367. dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
  368. __func__, host_control_smi_type);
  369. return -ENOSYS;
  370. }
  371. return 0;
  372. }
  373. /**
  374. * dcdbas_host_control: initiate host control
  375. *
  376. * This function is called by the driver after the system has
  377. * finished shutting down if the user application specified a
  378. * host control action to perform on shutdown. It is safe to
  379. * use smi_data_buf at this point because the system has finished
  380. * shutting down and no userspace apps are running.
  381. */
  382. static void dcdbas_host_control(void)
  383. {
  384. struct apm_cmd *apm_cmd;
  385. u8 action;
  386. if (host_control_action == HC_ACTION_NONE)
  387. return;
  388. action = host_control_action;
  389. host_control_action = HC_ACTION_NONE;
  390. if (!smi_data_buf) {
  391. dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
  392. return;
  393. }
  394. if (smi_data_buf_size < sizeof(struct apm_cmd)) {
  395. dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
  396. __func__);
  397. return;
  398. }
  399. apm_cmd = (struct apm_cmd *)smi_data_buf;
  400. /* power off takes precedence */
  401. if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
  402. apm_cmd->command = ESM_APM_POWER_CYCLE;
  403. apm_cmd->reserved = 0;
  404. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
  405. host_control_smi();
  406. } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
  407. apm_cmd->command = ESM_APM_POWER_CYCLE;
  408. apm_cmd->reserved = 0;
  409. *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
  410. host_control_smi();
  411. }
  412. }
  413. /**
  414. * dcdbas_reboot_notify: handle reboot notification for host control
  415. */
  416. static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
  417. void *unused)
  418. {
  419. switch (code) {
  420. case SYS_DOWN:
  421. case SYS_HALT:
  422. case SYS_POWER_OFF:
  423. if (host_control_on_shutdown) {
  424. /* firmware is going to perform host control action */
  425. printk(KERN_WARNING "Please wait for shutdown "
  426. "action to complete...\n");
  427. dcdbas_host_control();
  428. }
  429. break;
  430. }
  431. return NOTIFY_DONE;
  432. }
  433. static struct notifier_block dcdbas_reboot_nb = {
  434. .notifier_call = dcdbas_reboot_notify,
  435. .next = NULL,
  436. .priority = INT_MIN
  437. };
  438. static DCDBAS_BIN_ATTR_RW(smi_data);
  439. static struct bin_attribute *dcdbas_bin_attrs[] = {
  440. &bin_attr_smi_data,
  441. NULL
  442. };
  443. static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
  444. static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
  445. static DCDBAS_DEV_ATTR_WO(smi_request);
  446. static DCDBAS_DEV_ATTR_RW(host_control_action);
  447. static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
  448. static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
  449. static struct attribute *dcdbas_dev_attrs[] = {
  450. &dev_attr_smi_data_buf_size.attr,
  451. &dev_attr_smi_data_buf_phys_addr.attr,
  452. &dev_attr_smi_request.attr,
  453. &dev_attr_host_control_action.attr,
  454. &dev_attr_host_control_smi_type.attr,
  455. &dev_attr_host_control_on_shutdown.attr,
  456. NULL
  457. };
  458. static struct attribute_group dcdbas_attr_group = {
  459. .attrs = dcdbas_dev_attrs,
  460. .bin_attrs = dcdbas_bin_attrs,
  461. };
  462. static int dcdbas_probe(struct platform_device *dev)
  463. {
  464. int error;
  465. host_control_action = HC_ACTION_NONE;
  466. host_control_smi_type = HC_SMITYPE_NONE;
  467. dcdbas_pdev = dev;
  468. /*
  469. * BIOS SMI calls require buffer addresses be in 32-bit address space.
  470. * This is done by setting the DMA mask below.
  471. */
  472. error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
  473. if (error)
  474. return error;
  475. error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
  476. if (error)
  477. return error;
  478. register_reboot_notifier(&dcdbas_reboot_nb);
  479. dev_info(&dev->dev, "%s (version %s)\n",
  480. DRIVER_DESCRIPTION, DRIVER_VERSION);
  481. return 0;
  482. }
  483. static int dcdbas_remove(struct platform_device *dev)
  484. {
  485. unregister_reboot_notifier(&dcdbas_reboot_nb);
  486. sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
  487. return 0;
  488. }
  489. static struct platform_driver dcdbas_driver = {
  490. .driver = {
  491. .name = DRIVER_NAME,
  492. },
  493. .probe = dcdbas_probe,
  494. .remove = dcdbas_remove,
  495. };
  496. static const struct platform_device_info dcdbas_dev_info __initconst = {
  497. .name = DRIVER_NAME,
  498. .id = -1,
  499. .dma_mask = DMA_BIT_MASK(32),
  500. };
  501. static struct platform_device *dcdbas_pdev_reg;
  502. /**
  503. * dcdbas_init: initialize driver
  504. */
  505. static int __init dcdbas_init(void)
  506. {
  507. int error;
  508. error = platform_driver_register(&dcdbas_driver);
  509. if (error)
  510. return error;
  511. dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info);
  512. if (IS_ERR(dcdbas_pdev_reg)) {
  513. error = PTR_ERR(dcdbas_pdev_reg);
  514. goto err_unregister_driver;
  515. }
  516. return 0;
  517. err_unregister_driver:
  518. platform_driver_unregister(&dcdbas_driver);
  519. return error;
  520. }
  521. /**
  522. * dcdbas_exit: perform driver cleanup
  523. */
  524. static void __exit dcdbas_exit(void)
  525. {
  526. /*
  527. * make sure functions that use dcdbas_pdev are called
  528. * before platform_device_unregister
  529. */
  530. unregister_reboot_notifier(&dcdbas_reboot_nb);
  531. /*
  532. * We have to free the buffer here instead of dcdbas_remove
  533. * because only in module exit function we can be sure that
  534. * all sysfs attributes belonging to this module have been
  535. * released.
  536. */
  537. if (dcdbas_pdev)
  538. smi_data_buf_free();
  539. platform_device_unregister(dcdbas_pdev_reg);
  540. platform_driver_unregister(&dcdbas_driver);
  541. }
  542. module_init(dcdbas_init);
  543. module_exit(dcdbas_exit);
  544. MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
  545. MODULE_VERSION(DRIVER_VERSION);
  546. MODULE_AUTHOR("Dell Inc.");
  547. MODULE_LICENSE("GPL");
  548. /* Any System or BIOS claiming to be by Dell */
  549. MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");