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