osl.c 27 KB

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