osl.c 25 KB

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