osl.c 24 KB

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