osl.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. *
  26. */
  27. #include <linux/config.h>
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/mm.h>
  32. #include <linux/pci.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/kmod.h>
  36. #include <linux/delay.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/nmi.h>
  39. #include <acpi/acpi.h>
  40. #include <asm/io.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/processor.h>
  43. #include <asm/uaccess.h>
  44. #include <linux/efi.h>
  45. #define _COMPONENT ACPI_OS_SERVICES
  46. ACPI_MODULE_NAME("osl")
  47. #define PREFIX "ACPI: "
  48. struct acpi_os_dpc {
  49. acpi_osd_exec_callback function;
  50. void *context;
  51. };
  52. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  53. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  54. #endif
  55. #ifdef ENABLE_DEBUGGER
  56. #include <linux/kdb.h>
  57. /* stuff for debugger support */
  58. int acpi_in_debugger;
  59. EXPORT_SYMBOL(acpi_in_debugger);
  60. extern char line_buf[80];
  61. #endif /*ENABLE_DEBUGGER */
  62. int acpi_specific_hotkey_enabled = TRUE;
  63. EXPORT_SYMBOL(acpi_specific_hotkey_enabled);
  64. static unsigned int acpi_irq_irq;
  65. static acpi_osd_handler acpi_irq_handler;
  66. static void *acpi_irq_context;
  67. static struct workqueue_struct *kacpid_wq;
  68. acpi_status acpi_os_initialize(void)
  69. {
  70. return AE_OK;
  71. }
  72. acpi_status acpi_os_initialize1(void)
  73. {
  74. /*
  75. * Initialize PCI configuration space access, as we'll need to access
  76. * it while walking the namespace (bus 0 and root bridges w/ _BBNs).
  77. */
  78. if (!raw_pci_ops) {
  79. printk(KERN_ERR PREFIX
  80. "Access to PCI configuration space unavailable\n");
  81. return AE_NULL_ENTRY;
  82. }
  83. kacpid_wq = create_singlethread_workqueue("kacpid");
  84. BUG_ON(!kacpid_wq);
  85. return AE_OK;
  86. }
  87. acpi_status acpi_os_terminate(void)
  88. {
  89. if (acpi_irq_handler) {
  90. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  91. acpi_irq_handler);
  92. }
  93. destroy_workqueue(kacpid_wq);
  94. return AE_OK;
  95. }
  96. void acpi_os_printf(const char *fmt, ...)
  97. {
  98. va_list args;
  99. va_start(args, fmt);
  100. acpi_os_vprintf(fmt, args);
  101. va_end(args);
  102. }
  103. EXPORT_SYMBOL(acpi_os_printf);
  104. void acpi_os_vprintf(const char *fmt, va_list args)
  105. {
  106. static char buffer[512];
  107. vsprintf(buffer, fmt, args);
  108. #ifdef ENABLE_DEBUGGER
  109. if (acpi_in_debugger) {
  110. kdb_printf("%s", buffer);
  111. } else {
  112. printk("%s", buffer);
  113. }
  114. #else
  115. printk("%s", buffer);
  116. #endif
  117. }
  118. extern int acpi_in_resume;
  119. void *acpi_os_allocate(acpi_size size)
  120. {
  121. if (acpi_in_resume)
  122. return kmalloc(size, GFP_ATOMIC);
  123. else
  124. return kmalloc(size, GFP_KERNEL);
  125. }
  126. void acpi_os_free(void *ptr)
  127. {
  128. kfree(ptr);
  129. }
  130. EXPORT_SYMBOL(acpi_os_free);
  131. acpi_status acpi_os_get_root_pointer(u32 flags, struct acpi_pointer *addr)
  132. {
  133. if (efi_enabled) {
  134. addr->pointer_type = ACPI_PHYSICAL_POINTER;
  135. if (efi.acpi20)
  136. addr->pointer.physical =
  137. (acpi_physical_address) virt_to_phys(efi.acpi20);
  138. else if (efi.acpi)
  139. addr->pointer.physical =
  140. (acpi_physical_address) virt_to_phys(efi.acpi);
  141. else {
  142. printk(KERN_ERR PREFIX
  143. "System description tables not found\n");
  144. return AE_NOT_FOUND;
  145. }
  146. } else {
  147. if (ACPI_FAILURE(acpi_find_root_pointer(flags, addr))) {
  148. printk(KERN_ERR PREFIX
  149. "System description tables not found\n");
  150. return AE_NOT_FOUND;
  151. }
  152. }
  153. return AE_OK;
  154. }
  155. acpi_status
  156. acpi_os_map_memory(acpi_physical_address phys, acpi_size size,
  157. void __iomem ** virt)
  158. {
  159. if (efi_enabled) {
  160. if (EFI_MEMORY_WB & efi_mem_attributes(phys)) {
  161. *virt = (void __iomem *)phys_to_virt(phys);
  162. } else {
  163. *virt = ioremap(phys, size);
  164. }
  165. } else {
  166. if (phys > ULONG_MAX) {
  167. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  168. return AE_BAD_PARAMETER;
  169. }
  170. /*
  171. * ioremap checks to ensure this is in reserved space
  172. */
  173. *virt = ioremap((unsigned long)phys, size);
  174. }
  175. if (!*virt)
  176. return AE_NO_MEMORY;
  177. return AE_OK;
  178. }
  179. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  180. {
  181. iounmap(virt);
  182. }
  183. #ifdef ACPI_FUTURE_USAGE
  184. acpi_status
  185. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  186. {
  187. if (!phys || !virt)
  188. return AE_BAD_PARAMETER;
  189. *phys = virt_to_phys(virt);
  190. return AE_OK;
  191. }
  192. #endif
  193. #define ACPI_MAX_OVERRIDE_LEN 100
  194. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  195. acpi_status
  196. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  197. acpi_string * new_val)
  198. {
  199. if (!init_val || !new_val)
  200. return AE_BAD_PARAMETER;
  201. *new_val = NULL;
  202. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  203. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  204. acpi_os_name);
  205. *new_val = acpi_os_name;
  206. }
  207. return AE_OK;
  208. }
  209. acpi_status
  210. acpi_os_table_override(struct acpi_table_header * existing_table,
  211. struct acpi_table_header ** new_table)
  212. {
  213. if (!existing_table || !new_table)
  214. return AE_BAD_PARAMETER;
  215. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  216. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  217. *new_table = (struct acpi_table_header *)AmlCode;
  218. else
  219. *new_table = NULL;
  220. #else
  221. *new_table = NULL;
  222. #endif
  223. return AE_OK;
  224. }
  225. static irqreturn_t acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
  226. {
  227. return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
  228. }
  229. acpi_status
  230. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  231. void *context)
  232. {
  233. unsigned int irq;
  234. /*
  235. * Ignore the GSI from the core, and use the value in our copy of the
  236. * FADT. It may not be the same if an interrupt source override exists
  237. * for the SCI.
  238. */
  239. gsi = acpi_fadt.sci_int;
  240. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  241. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  242. gsi);
  243. return AE_OK;
  244. }
  245. acpi_irq_handler = handler;
  246. acpi_irq_context = context;
  247. if (request_irq(irq, acpi_irq, SA_SHIRQ, "acpi", acpi_irq)) {
  248. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  249. return AE_NOT_ACQUIRED;
  250. }
  251. acpi_irq_irq = irq;
  252. return AE_OK;
  253. }
  254. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  255. {
  256. if (irq) {
  257. free_irq(irq, acpi_irq);
  258. acpi_irq_handler = NULL;
  259. acpi_irq_irq = 0;
  260. }
  261. return AE_OK;
  262. }
  263. /*
  264. * Running in interpreter thread context, safe to sleep
  265. */
  266. void acpi_os_sleep(acpi_integer ms)
  267. {
  268. current->state = TASK_INTERRUPTIBLE;
  269. schedule_timeout(((signed long)ms * HZ) / 1000);
  270. }
  271. EXPORT_SYMBOL(acpi_os_sleep);
  272. void acpi_os_stall(u32 us)
  273. {
  274. while (us) {
  275. u32 delay = 1000;
  276. if (delay > us)
  277. delay = us;
  278. udelay(delay);
  279. touch_nmi_watchdog();
  280. us -= delay;
  281. }
  282. }
  283. EXPORT_SYMBOL(acpi_os_stall);
  284. /*
  285. * Support ACPI 3.0 AML Timer operand
  286. * Returns 64-bit free-running, monotonically increasing timer
  287. * with 100ns granularity
  288. */
  289. u64 acpi_os_get_timer(void)
  290. {
  291. static u64 t;
  292. #ifdef CONFIG_HPET
  293. /* TBD: use HPET if available */
  294. #endif
  295. #ifdef CONFIG_X86_PM_TIMER
  296. /* TBD: default to PM timer if HPET was not available */
  297. #endif
  298. if (!t)
  299. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  300. return ++t;
  301. }
  302. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  303. {
  304. u32 dummy;
  305. if (!value)
  306. value = &dummy;
  307. switch (width) {
  308. case 8:
  309. *(u8 *) value = inb(port);
  310. break;
  311. case 16:
  312. *(u16 *) value = inw(port);
  313. break;
  314. case 32:
  315. *(u32 *) value = inl(port);
  316. break;
  317. default:
  318. BUG();
  319. }
  320. return AE_OK;
  321. }
  322. EXPORT_SYMBOL(acpi_os_read_port);
  323. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  324. {
  325. switch (width) {
  326. case 8:
  327. outb(value, port);
  328. break;
  329. case 16:
  330. outw(value, port);
  331. break;
  332. case 32:
  333. outl(value, port);
  334. break;
  335. default:
  336. BUG();
  337. }
  338. return AE_OK;
  339. }
  340. EXPORT_SYMBOL(acpi_os_write_port);
  341. acpi_status
  342. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  343. {
  344. u32 dummy;
  345. void __iomem *virt_addr;
  346. int iomem = 0;
  347. if (efi_enabled) {
  348. if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
  349. /* HACK ALERT! We can use readb/w/l on real memory too.. */
  350. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  351. } else {
  352. iomem = 1;
  353. virt_addr = ioremap(phys_addr, width);
  354. }
  355. } else
  356. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  357. if (!value)
  358. value = &dummy;
  359. switch (width) {
  360. case 8:
  361. *(u8 *) value = readb(virt_addr);
  362. break;
  363. case 16:
  364. *(u16 *) value = readw(virt_addr);
  365. break;
  366. case 32:
  367. *(u32 *) value = readl(virt_addr);
  368. break;
  369. default:
  370. BUG();
  371. }
  372. if (efi_enabled) {
  373. if (iomem)
  374. iounmap(virt_addr);
  375. }
  376. return AE_OK;
  377. }
  378. acpi_status
  379. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  380. {
  381. void __iomem *virt_addr;
  382. int iomem = 0;
  383. if (efi_enabled) {
  384. if (EFI_MEMORY_WB & efi_mem_attributes(phys_addr)) {
  385. /* HACK ALERT! We can use writeb/w/l on real memory too */
  386. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  387. } else {
  388. iomem = 1;
  389. virt_addr = ioremap(phys_addr, width);
  390. }
  391. } else
  392. virt_addr = (void __iomem *)phys_to_virt(phys_addr);
  393. switch (width) {
  394. case 8:
  395. writeb(value, virt_addr);
  396. break;
  397. case 16:
  398. writew(value, virt_addr);
  399. break;
  400. case 32:
  401. writel(value, virt_addr);
  402. break;
  403. default:
  404. BUG();
  405. }
  406. if (iomem)
  407. iounmap(virt_addr);
  408. return AE_OK;
  409. }
  410. acpi_status
  411. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  412. void *value, u32 width)
  413. {
  414. int result, size;
  415. if (!value)
  416. return AE_BAD_PARAMETER;
  417. switch (width) {
  418. case 8:
  419. size = 1;
  420. break;
  421. case 16:
  422. size = 2;
  423. break;
  424. case 32:
  425. size = 4;
  426. break;
  427. default:
  428. return AE_ERROR;
  429. }
  430. BUG_ON(!raw_pci_ops);
  431. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  432. PCI_DEVFN(pci_id->device, pci_id->function),
  433. reg, size, value);
  434. return (result ? AE_ERROR : AE_OK);
  435. }
  436. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  437. acpi_status
  438. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  439. acpi_integer value, u32 width)
  440. {
  441. int result, size;
  442. switch (width) {
  443. case 8:
  444. size = 1;
  445. break;
  446. case 16:
  447. size = 2;
  448. break;
  449. case 32:
  450. size = 4;
  451. break;
  452. default:
  453. return AE_ERROR;
  454. }
  455. BUG_ON(!raw_pci_ops);
  456. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  457. PCI_DEVFN(pci_id->device, pci_id->function),
  458. reg, size, value);
  459. return (result ? AE_ERROR : AE_OK);
  460. }
  461. /* TODO: Change code to take advantage of driver model more */
  462. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  463. acpi_handle chandle, /* current node */
  464. struct acpi_pci_id **id,
  465. int *is_bridge, u8 * bus_number)
  466. {
  467. acpi_handle handle;
  468. struct acpi_pci_id *pci_id = *id;
  469. acpi_status status;
  470. unsigned long temp;
  471. acpi_object_type type;
  472. u8 tu8;
  473. acpi_get_parent(chandle, &handle);
  474. if (handle != rhandle) {
  475. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  476. bus_number);
  477. status = acpi_get_type(handle, &type);
  478. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  479. return;
  480. status =
  481. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  482. &temp);
  483. if (ACPI_SUCCESS(status)) {
  484. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  485. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  486. if (*is_bridge)
  487. pci_id->bus = *bus_number;
  488. /* any nicer way to get bus number of bridge ? */
  489. status =
  490. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  491. 8);
  492. if (ACPI_SUCCESS(status)
  493. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  494. status =
  495. acpi_os_read_pci_configuration(pci_id, 0x18,
  496. &tu8, 8);
  497. if (!ACPI_SUCCESS(status)) {
  498. /* Certainly broken... FIX ME */
  499. return;
  500. }
  501. *is_bridge = 1;
  502. pci_id->bus = tu8;
  503. status =
  504. acpi_os_read_pci_configuration(pci_id, 0x19,
  505. &tu8, 8);
  506. if (ACPI_SUCCESS(status)) {
  507. *bus_number = tu8;
  508. }
  509. } else
  510. *is_bridge = 0;
  511. }
  512. }
  513. }
  514. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  515. acpi_handle chandle, /* current node */
  516. struct acpi_pci_id **id)
  517. {
  518. int is_bridge = 1;
  519. u8 bus_number = (*id)->bus;
  520. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  521. }
  522. static void acpi_os_execute_deferred(void *context)
  523. {
  524. struct acpi_os_dpc *dpc = NULL;
  525. ACPI_FUNCTION_TRACE("os_execute_deferred");
  526. dpc = (struct acpi_os_dpc *)context;
  527. if (!dpc) {
  528. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
  529. return_VOID;
  530. }
  531. dpc->function(dpc->context);
  532. kfree(dpc);
  533. return_VOID;
  534. }
  535. acpi_status
  536. acpi_os_queue_for_execution(u32 priority,
  537. acpi_osd_exec_callback function, void *context)
  538. {
  539. acpi_status status = AE_OK;
  540. struct acpi_os_dpc *dpc;
  541. struct work_struct *task;
  542. ACPI_FUNCTION_TRACE("os_queue_for_execution");
  543. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  544. "Scheduling function [%p(%p)] for deferred execution.\n",
  545. function, context));
  546. if (!function)
  547. return_ACPI_STATUS(AE_BAD_PARAMETER);
  548. /*
  549. * Allocate/initialize DPC structure. Note that this memory will be
  550. * freed by the callee. The kernel handles the tq_struct list in a
  551. * way that allows us to also free its memory inside the callee.
  552. * Because we may want to schedule several tasks with different
  553. * parameters we can't use the approach some kernel code uses of
  554. * having a static tq_struct.
  555. * We can save time and code by allocating the DPC and tq_structs
  556. * from the same memory.
  557. */
  558. dpc =
  559. kmalloc(sizeof(struct acpi_os_dpc) + sizeof(struct work_struct),
  560. GFP_ATOMIC);
  561. if (!dpc)
  562. return_ACPI_STATUS(AE_NO_MEMORY);
  563. dpc->function = function;
  564. dpc->context = context;
  565. task = (void *)(dpc + 1);
  566. INIT_WORK(task, acpi_os_execute_deferred, (void *)dpc);
  567. if (!queue_work(kacpid_wq, task)) {
  568. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  569. "Call to queue_work() failed.\n"));
  570. kfree(dpc);
  571. status = AE_ERROR;
  572. }
  573. return_ACPI_STATUS(status);
  574. }
  575. EXPORT_SYMBOL(acpi_os_queue_for_execution);
  576. void acpi_os_wait_events_complete(void *context)
  577. {
  578. flush_workqueue(kacpid_wq);
  579. }
  580. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  581. /*
  582. * Allocate the memory for a spinlock and initialize it.
  583. */
  584. acpi_status acpi_os_create_lock(acpi_handle * out_handle)
  585. {
  586. spinlock_t *lock_ptr;
  587. ACPI_FUNCTION_TRACE("os_create_lock");
  588. lock_ptr = acpi_os_allocate(sizeof(spinlock_t));
  589. spin_lock_init(lock_ptr);
  590. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating spinlock[%p].\n", lock_ptr));
  591. *out_handle = lock_ptr;
  592. return_ACPI_STATUS(AE_OK);
  593. }
  594. /*
  595. * Deallocate the memory for a spinlock.
  596. */
  597. void acpi_os_delete_lock(acpi_handle handle)
  598. {
  599. ACPI_FUNCTION_TRACE("os_create_lock");
  600. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting spinlock[%p].\n", handle));
  601. acpi_os_free(handle);
  602. return_VOID;
  603. }
  604. acpi_status
  605. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  606. {
  607. struct semaphore *sem = NULL;
  608. ACPI_FUNCTION_TRACE("os_create_semaphore");
  609. sem = acpi_os_allocate(sizeof(struct semaphore));
  610. if (!sem)
  611. return_ACPI_STATUS(AE_NO_MEMORY);
  612. memset(sem, 0, sizeof(struct semaphore));
  613. sema_init(sem, initial_units);
  614. *handle = (acpi_handle *) sem;
  615. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  616. *handle, initial_units));
  617. return_ACPI_STATUS(AE_OK);
  618. }
  619. EXPORT_SYMBOL(acpi_os_create_semaphore);
  620. /*
  621. * TODO: A better way to delete semaphores? Linux doesn't have a
  622. * 'delete_semaphore()' function -- may result in an invalid
  623. * pointer dereference for non-synchronized consumers. Should
  624. * we at least check for blocked threads and signal/cancel them?
  625. */
  626. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  627. {
  628. struct semaphore *sem = (struct semaphore *)handle;
  629. ACPI_FUNCTION_TRACE("os_delete_semaphore");
  630. if (!sem)
  631. return_ACPI_STATUS(AE_BAD_PARAMETER);
  632. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  633. acpi_os_free(sem);
  634. sem = NULL;
  635. return_ACPI_STATUS(AE_OK);
  636. }
  637. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  638. /*
  639. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  640. * improvise. The process is to sleep for one scheduler quantum
  641. * until the semaphore becomes available. Downside is that this
  642. * may result in starvation for timeout-based waits when there's
  643. * lots of semaphore activity.
  644. *
  645. * TODO: Support for units > 1?
  646. */
  647. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  648. {
  649. acpi_status status = AE_OK;
  650. struct semaphore *sem = (struct semaphore *)handle;
  651. int ret = 0;
  652. ACPI_FUNCTION_TRACE("os_wait_semaphore");
  653. if (!sem || (units < 1))
  654. return_ACPI_STATUS(AE_BAD_PARAMETER);
  655. if (units > 1)
  656. return_ACPI_STATUS(AE_SUPPORT);
  657. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  658. handle, units, timeout));
  659. if (in_atomic())
  660. timeout = 0;
  661. switch (timeout) {
  662. /*
  663. * No Wait:
  664. * --------
  665. * A zero timeout value indicates that we shouldn't wait - just
  666. * acquire the semaphore if available otherwise return AE_TIME
  667. * (a.k.a. 'would block').
  668. */
  669. case 0:
  670. if (down_trylock(sem))
  671. status = AE_TIME;
  672. break;
  673. /*
  674. * Wait Indefinitely:
  675. * ------------------
  676. */
  677. case ACPI_WAIT_FOREVER:
  678. down(sem);
  679. break;
  680. /*
  681. * Wait w/ Timeout:
  682. * ----------------
  683. */
  684. default:
  685. // TODO: A better timeout algorithm?
  686. {
  687. int i = 0;
  688. static const int quantum_ms = 1000 / HZ;
  689. ret = down_trylock(sem);
  690. for (i = timeout; (i > 0 && ret < 0); i -= quantum_ms) {
  691. current->state = TASK_INTERRUPTIBLE;
  692. schedule_timeout(1);
  693. ret = down_trylock(sem);
  694. }
  695. if (ret != 0)
  696. status = AE_TIME;
  697. }
  698. break;
  699. }
  700. if (ACPI_FAILURE(status)) {
  701. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  702. "Failed to acquire semaphore[%p|%d|%d], %s\n",
  703. handle, units, timeout,
  704. acpi_format_exception(status)));
  705. } else {
  706. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  707. "Acquired semaphore[%p|%d|%d]\n", handle,
  708. units, timeout));
  709. }
  710. return_ACPI_STATUS(status);
  711. }
  712. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  713. /*
  714. * TODO: Support for units > 1?
  715. */
  716. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  717. {
  718. struct semaphore *sem = (struct semaphore *)handle;
  719. ACPI_FUNCTION_TRACE("os_signal_semaphore");
  720. if (!sem || (units < 1))
  721. return_ACPI_STATUS(AE_BAD_PARAMETER);
  722. if (units > 1)
  723. return_ACPI_STATUS(AE_SUPPORT);
  724. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  725. units));
  726. up(sem);
  727. return_ACPI_STATUS(AE_OK);
  728. }
  729. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  730. #ifdef ACPI_FUTURE_USAGE
  731. u32 acpi_os_get_line(char *buffer)
  732. {
  733. #ifdef ENABLE_DEBUGGER
  734. if (acpi_in_debugger) {
  735. u32 chars;
  736. kdb_read(buffer, sizeof(line_buf));
  737. /* remove the CR kdb includes */
  738. chars = strlen(buffer) - 1;
  739. buffer[chars] = '\0';
  740. }
  741. #endif
  742. return 0;
  743. }
  744. #endif /* ACPI_FUTURE_USAGE */
  745. /* Assumes no unreadable holes inbetween */
  746. u8 acpi_os_readable(void *ptr, acpi_size len)
  747. {
  748. #if defined(__i386__) || defined(__x86_64__)
  749. char tmp;
  750. return !__get_user(tmp, (char __user *)ptr)
  751. && !__get_user(tmp, (char __user *)ptr + len - 1);
  752. #endif
  753. return 1;
  754. }
  755. #ifdef ACPI_FUTURE_USAGE
  756. u8 acpi_os_writable(void *ptr, acpi_size len)
  757. {
  758. /* could do dummy write (racy) or a kernel page table lookup.
  759. The later may be difficult at early boot when kmap doesn't work yet. */
  760. return 1;
  761. }
  762. #endif
  763. u32 acpi_os_get_thread_id(void)
  764. {
  765. if (!in_atomic())
  766. return current->pid;
  767. return 0;
  768. }
  769. acpi_status acpi_os_signal(u32 function, void *info)
  770. {
  771. switch (function) {
  772. case ACPI_SIGNAL_FATAL:
  773. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  774. break;
  775. case ACPI_SIGNAL_BREAKPOINT:
  776. /*
  777. * AML Breakpoint
  778. * ACPI spec. says to treat it as a NOP unless
  779. * you are debugging. So if/when we integrate
  780. * AML debugger into the kernel debugger its
  781. * hook will go here. But until then it is
  782. * not useful to print anything on breakpoints.
  783. */
  784. break;
  785. default:
  786. break;
  787. }
  788. return AE_OK;
  789. }
  790. EXPORT_SYMBOL(acpi_os_signal);
  791. static int __init acpi_os_name_setup(char *str)
  792. {
  793. char *p = acpi_os_name;
  794. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  795. if (!str || !*str)
  796. return 0;
  797. for (; count-- && str && *str; str++) {
  798. if (isalnum(*str) || *str == ' ' || *str == ':')
  799. *p++ = *str;
  800. else if (*str == '\'' || *str == '"')
  801. continue;
  802. else
  803. break;
  804. }
  805. *p = 0;
  806. return 1;
  807. }
  808. __setup("acpi_os_name=", acpi_os_name_setup);
  809. /*
  810. * _OSI control
  811. * empty string disables _OSI
  812. * TBD additional string adds to _OSI
  813. */
  814. static int __init acpi_osi_setup(char *str)
  815. {
  816. if (str == NULL || *str == '\0') {
  817. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  818. acpi_gbl_create_osi_method = FALSE;
  819. } else {
  820. /* TBD */
  821. printk(KERN_ERR PREFIX "_OSI additional string ignored -- %s\n",
  822. str);
  823. }
  824. return 1;
  825. }
  826. __setup("acpi_osi=", acpi_osi_setup);
  827. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  828. static int __init acpi_serialize_setup(char *str)
  829. {
  830. printk(KERN_INFO PREFIX "serialize enabled\n");
  831. acpi_gbl_all_methods_serialized = TRUE;
  832. return 1;
  833. }
  834. __setup("acpi_serialize", acpi_serialize_setup);
  835. /*
  836. * Wake and Run-Time GPES are expected to be separate.
  837. * We disable wake-GPEs at run-time to prevent spurious
  838. * interrupts.
  839. *
  840. * However, if a system exists that shares Wake and
  841. * Run-time events on the same GPE this flag is available
  842. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  843. */
  844. static int __init acpi_wake_gpes_always_on_setup(char *str)
  845. {
  846. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  847. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  848. return 1;
  849. }
  850. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  851. static int __init acpi_hotkey_setup(char *str)
  852. {
  853. acpi_specific_hotkey_enabled = FALSE;
  854. return 1;
  855. }
  856. __setup("acpi_generic_hotkey", acpi_hotkey_setup);
  857. /*
  858. * max_cstate is defined in the base kernel so modules can
  859. * change it w/o depending on the state of the processor module.
  860. */
  861. unsigned int max_cstate = ACPI_PROCESSOR_MAX_POWER;
  862. EXPORT_SYMBOL(max_cstate);
  863. /*
  864. * Acquire a spinlock.
  865. *
  866. * handle is a pointer to the spinlock_t.
  867. * flags is *not* the result of save_flags - it is an ACPI-specific flag variable
  868. * that indicates whether we are at interrupt level.
  869. */
  870. unsigned long acpi_os_acquire_lock(acpi_handle handle)
  871. {
  872. unsigned long flags;
  873. spin_lock_irqsave((spinlock_t *) handle, flags);
  874. return flags;
  875. }
  876. /*
  877. * Release a spinlock. See above.
  878. */
  879. void acpi_os_release_lock(acpi_handle handle, unsigned long flags)
  880. {
  881. spin_unlock_irqrestore((spinlock_t *) handle, flags);
  882. }
  883. #ifndef ACPI_USE_LOCAL_CACHE
  884. /*******************************************************************************
  885. *
  886. * FUNCTION: acpi_os_create_cache
  887. *
  888. * PARAMETERS: CacheName - Ascii name for the cache
  889. * ObjectSize - Size of each cached object
  890. * MaxDepth - Maximum depth of the cache (in objects)
  891. * ReturnCache - Where the new cache object is returned
  892. *
  893. * RETURN: Status
  894. *
  895. * DESCRIPTION: Create a cache object
  896. *
  897. ******************************************************************************/
  898. acpi_status
  899. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  900. {
  901. *cache = kmem_cache_create(name, size, 0, 0, NULL, NULL);
  902. return AE_OK;
  903. }
  904. /*******************************************************************************
  905. *
  906. * FUNCTION: acpi_os_purge_cache
  907. *
  908. * PARAMETERS: Cache - Handle to cache object
  909. *
  910. * RETURN: Status
  911. *
  912. * DESCRIPTION: Free all objects within the requested cache.
  913. *
  914. ******************************************************************************/
  915. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  916. {
  917. (void)kmem_cache_shrink(cache);
  918. return (AE_OK);
  919. }
  920. /*******************************************************************************
  921. *
  922. * FUNCTION: acpi_os_delete_cache
  923. *
  924. * PARAMETERS: Cache - Handle to cache object
  925. *
  926. * RETURN: Status
  927. *
  928. * DESCRIPTION: Free all objects within the requested cache and delete the
  929. * cache object.
  930. *
  931. ******************************************************************************/
  932. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  933. {
  934. (void)kmem_cache_destroy(cache);
  935. return (AE_OK);
  936. }
  937. /*******************************************************************************
  938. *
  939. * FUNCTION: acpi_os_release_object
  940. *
  941. * PARAMETERS: Cache - Handle to cache object
  942. * Object - The object to be released
  943. *
  944. * RETURN: None
  945. *
  946. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  947. * the object is deleted.
  948. *
  949. ******************************************************************************/
  950. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  951. {
  952. kmem_cache_free(cache, object);
  953. return (AE_OK);
  954. }
  955. /*******************************************************************************
  956. *
  957. * FUNCTION: acpi_os_acquire_object
  958. *
  959. * PARAMETERS: Cache - Handle to cache object
  960. * ReturnObject - Where the object is returned
  961. *
  962. * RETURN: Status
  963. *
  964. * DESCRIPTION: Get an object from the specified cache. If cache is empty,
  965. * the object is allocated.
  966. *
  967. ******************************************************************************/
  968. void *acpi_os_acquire_object(acpi_cache_t * cache)
  969. {
  970. void *object = kmem_cache_alloc(cache, GFP_KERNEL);
  971. WARN_ON(!object);
  972. return object;
  973. }
  974. #endif