osl.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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 *__init_refok
  179. acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  180. {
  181. if (phys > ULONG_MAX) {
  182. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  183. return NULL;
  184. }
  185. if (acpi_gbl_permanent_mmap)
  186. /*
  187. * ioremap checks to ensure this is in reserved space
  188. */
  189. return ioremap((unsigned long)phys, size);
  190. else
  191. return __acpi_map_table((unsigned long)phys, size);
  192. }
  193. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  194. void acpi_os_unmap_memory(void __iomem * virt, acpi_size size)
  195. {
  196. if (acpi_gbl_permanent_mmap) {
  197. iounmap(virt);
  198. }
  199. }
  200. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  201. #ifdef ACPI_FUTURE_USAGE
  202. acpi_status
  203. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  204. {
  205. if (!phys || !virt)
  206. return AE_BAD_PARAMETER;
  207. *phys = virt_to_phys(virt);
  208. return AE_OK;
  209. }
  210. #endif
  211. #define ACPI_MAX_OVERRIDE_LEN 100
  212. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  213. acpi_status
  214. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  215. acpi_string * new_val)
  216. {
  217. if (!init_val || !new_val)
  218. return AE_BAD_PARAMETER;
  219. *new_val = NULL;
  220. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  221. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  222. acpi_os_name);
  223. *new_val = acpi_os_name;
  224. }
  225. return AE_OK;
  226. }
  227. acpi_status
  228. acpi_os_table_override(struct acpi_table_header * existing_table,
  229. struct acpi_table_header ** new_table)
  230. {
  231. if (!existing_table || !new_table)
  232. return AE_BAD_PARAMETER;
  233. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  234. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  235. *new_table = (struct acpi_table_header *)AmlCode;
  236. else
  237. *new_table = NULL;
  238. #else
  239. *new_table = NULL;
  240. #endif
  241. return AE_OK;
  242. }
  243. static irqreturn_t acpi_irq(int irq, void *dev_id)
  244. {
  245. return (*acpi_irq_handler) (acpi_irq_context) ? IRQ_HANDLED : IRQ_NONE;
  246. }
  247. acpi_status
  248. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  249. void *context)
  250. {
  251. unsigned int irq;
  252. /*
  253. * Ignore the GSI from the core, and use the value in our copy of the
  254. * FADT. It may not be the same if an interrupt source override exists
  255. * for the SCI.
  256. */
  257. gsi = acpi_gbl_FADT.sci_interrupt;
  258. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  259. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  260. gsi);
  261. return AE_OK;
  262. }
  263. acpi_irq_handler = handler;
  264. acpi_irq_context = context;
  265. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  266. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  267. return AE_NOT_ACQUIRED;
  268. }
  269. acpi_irq_irq = irq;
  270. return AE_OK;
  271. }
  272. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  273. {
  274. if (irq) {
  275. free_irq(irq, acpi_irq);
  276. acpi_irq_handler = NULL;
  277. acpi_irq_irq = 0;
  278. }
  279. return AE_OK;
  280. }
  281. /*
  282. * Running in interpreter thread context, safe to sleep
  283. */
  284. void acpi_os_sleep(acpi_integer ms)
  285. {
  286. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  287. }
  288. EXPORT_SYMBOL(acpi_os_sleep);
  289. void acpi_os_stall(u32 us)
  290. {
  291. while (us) {
  292. u32 delay = 1000;
  293. if (delay > us)
  294. delay = us;
  295. udelay(delay);
  296. touch_nmi_watchdog();
  297. us -= delay;
  298. }
  299. }
  300. EXPORT_SYMBOL(acpi_os_stall);
  301. /*
  302. * Support ACPI 3.0 AML Timer operand
  303. * Returns 64-bit free-running, monotonically increasing timer
  304. * with 100ns granularity
  305. */
  306. u64 acpi_os_get_timer(void)
  307. {
  308. static u64 t;
  309. #ifdef CONFIG_HPET
  310. /* TBD: use HPET if available */
  311. #endif
  312. #ifdef CONFIG_X86_PM_TIMER
  313. /* TBD: default to PM timer if HPET was not available */
  314. #endif
  315. if (!t)
  316. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  317. return ++t;
  318. }
  319. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  320. {
  321. u32 dummy;
  322. if (!value)
  323. value = &dummy;
  324. *value = 0;
  325. if (width <= 8) {
  326. *(u8 *) value = inb(port);
  327. } else if (width <= 16) {
  328. *(u16 *) value = inw(port);
  329. } else if (width <= 32) {
  330. *(u32 *) value = inl(port);
  331. } else {
  332. BUG();
  333. }
  334. return AE_OK;
  335. }
  336. EXPORT_SYMBOL(acpi_os_read_port);
  337. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  338. {
  339. if (width <= 8) {
  340. outb(value, port);
  341. } else if (width <= 16) {
  342. outw(value, port);
  343. } else if (width <= 32) {
  344. outl(value, port);
  345. } else {
  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. static void enable_osi_linux(int enable) {
  791. if (osi_linux != enable)
  792. printk(KERN_INFO PREFIX "%sabled _OSI(Linux)\n",
  793. enable ? "En": "Dis");
  794. osi_linux = enable;
  795. return;
  796. }
  797. /*
  798. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  799. *
  800. * empty string disables _OSI
  801. * string starting with '!' disables that string
  802. * otherwise string is added to list, augmenting built-in strings
  803. */
  804. static int __init acpi_osi_setup(char *str)
  805. {
  806. if (str == NULL || *str == '\0') {
  807. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  808. acpi_gbl_create_osi_method = FALSE;
  809. } else if (!strcmp("!Linux", str)) {
  810. enable_osi_linux(0);
  811. } else if (*str == '!') {
  812. if (acpi_osi_invalidate(++str) == AE_OK)
  813. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  814. } else if (!strcmp("Linux", str)) {
  815. enable_osi_linux(1);
  816. } else if (*osi_additional_string == '\0') {
  817. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  818. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  819. }
  820. return 1;
  821. }
  822. __setup("acpi_osi=", acpi_osi_setup);
  823. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  824. static int __init acpi_serialize_setup(char *str)
  825. {
  826. printk(KERN_INFO PREFIX "serialize enabled\n");
  827. acpi_gbl_all_methods_serialized = TRUE;
  828. return 1;
  829. }
  830. __setup("acpi_serialize", acpi_serialize_setup);
  831. /*
  832. * Wake and Run-Time GPES are expected to be separate.
  833. * We disable wake-GPEs at run-time to prevent spurious
  834. * interrupts.
  835. *
  836. * However, if a system exists that shares Wake and
  837. * Run-time events on the same GPE this flag is available
  838. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  839. */
  840. static int __init acpi_wake_gpes_always_on_setup(char *str)
  841. {
  842. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  843. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  844. return 1;
  845. }
  846. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  847. /*
  848. * Acquire a spinlock.
  849. *
  850. * handle is a pointer to the spinlock_t.
  851. */
  852. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  853. {
  854. acpi_cpu_flags flags;
  855. spin_lock_irqsave(lockp, flags);
  856. return flags;
  857. }
  858. /*
  859. * Release a spinlock. See above.
  860. */
  861. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  862. {
  863. spin_unlock_irqrestore(lockp, flags);
  864. }
  865. #ifndef ACPI_USE_LOCAL_CACHE
  866. /*******************************************************************************
  867. *
  868. * FUNCTION: acpi_os_create_cache
  869. *
  870. * PARAMETERS: name - Ascii name for the cache
  871. * size - Size of each cached object
  872. * depth - Maximum depth of the cache (in objects) <ignored>
  873. * cache - Where the new cache object is returned
  874. *
  875. * RETURN: status
  876. *
  877. * DESCRIPTION: Create a cache object
  878. *
  879. ******************************************************************************/
  880. acpi_status
  881. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  882. {
  883. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  884. if (*cache == NULL)
  885. return AE_ERROR;
  886. else
  887. return AE_OK;
  888. }
  889. /*******************************************************************************
  890. *
  891. * FUNCTION: acpi_os_purge_cache
  892. *
  893. * PARAMETERS: Cache - Handle to cache object
  894. *
  895. * RETURN: Status
  896. *
  897. * DESCRIPTION: Free all objects within the requested cache.
  898. *
  899. ******************************************************************************/
  900. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  901. {
  902. kmem_cache_shrink(cache);
  903. return (AE_OK);
  904. }
  905. /*******************************************************************************
  906. *
  907. * FUNCTION: acpi_os_delete_cache
  908. *
  909. * PARAMETERS: Cache - Handle to cache object
  910. *
  911. * RETURN: Status
  912. *
  913. * DESCRIPTION: Free all objects within the requested cache and delete the
  914. * cache object.
  915. *
  916. ******************************************************************************/
  917. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  918. {
  919. kmem_cache_destroy(cache);
  920. return (AE_OK);
  921. }
  922. /*******************************************************************************
  923. *
  924. * FUNCTION: acpi_os_release_object
  925. *
  926. * PARAMETERS: Cache - Handle to cache object
  927. * Object - The object to be released
  928. *
  929. * RETURN: None
  930. *
  931. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  932. * the object is deleted.
  933. *
  934. ******************************************************************************/
  935. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  936. {
  937. kmem_cache_free(cache, object);
  938. return (AE_OK);
  939. }
  940. /******************************************************************************
  941. *
  942. * FUNCTION: acpi_os_validate_interface
  943. *
  944. * PARAMETERS: interface - Requested interface to be validated
  945. *
  946. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  947. *
  948. * DESCRIPTION: Match an interface string to the interfaces supported by the
  949. * host. Strings originate from an AML call to the _OSI method.
  950. *
  951. *****************************************************************************/
  952. acpi_status
  953. acpi_os_validate_interface (char *interface)
  954. {
  955. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  956. return AE_OK;
  957. if (!strcmp("Linux", interface)) {
  958. printk(KERN_WARNING PREFIX
  959. "System BIOS is requesting _OSI(Linux)\n");
  960. printk(KERN_WARNING PREFIX
  961. "If \"acpi_osi=Linux\" works better,\n"
  962. "Please send dmidecode "
  963. "to linux-acpi@vger.kernel.org\n");
  964. if(osi_linux)
  965. return AE_OK;
  966. }
  967. return AE_SUPPORT;
  968. }
  969. /******************************************************************************
  970. *
  971. * FUNCTION: acpi_os_validate_address
  972. *
  973. * PARAMETERS: space_id - ACPI space ID
  974. * address - Physical address
  975. * length - Address length
  976. *
  977. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  978. * should return AE_AML_ILLEGAL_ADDRESS.
  979. *
  980. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  981. * the addresses accessed by AML operation regions.
  982. *
  983. *****************************************************************************/
  984. acpi_status
  985. acpi_os_validate_address (
  986. u8 space_id,
  987. acpi_physical_address address,
  988. acpi_size length)
  989. {
  990. return AE_OK;
  991. }
  992. #ifdef CONFIG_DMI
  993. static int dmi_osi_linux(const struct dmi_system_id *d)
  994. {
  995. printk(KERN_NOTICE "%s detected: enabling _OSI(Linux)\n", d->ident);
  996. enable_osi_linux(1);
  997. return 0;
  998. }
  999. static struct dmi_system_id acpi_osl_dmi_table[] __initdata = {
  1000. /*
  1001. * Boxes that need _OSI(Linux)
  1002. */
  1003. {
  1004. .callback = dmi_osi_linux,
  1005. .ident = "Intel Napa CRB",
  1006. .matches = {
  1007. DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
  1008. DMI_MATCH(DMI_BOARD_NAME, "MPAD-MSAE Customer Reference Boards"),
  1009. },
  1010. },
  1011. {}
  1012. };
  1013. #endif /* CONFIG_DMI */
  1014. #endif