osl.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. switch (width) {
  324. case 8:
  325. *(u8 *) value = inb(port);
  326. break;
  327. case 16:
  328. *(u16 *) value = inw(port);
  329. break;
  330. case 32:
  331. *(u32 *) value = inl(port);
  332. break;
  333. default:
  334. BUG();
  335. }
  336. return AE_OK;
  337. }
  338. EXPORT_SYMBOL(acpi_os_read_port);
  339. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  340. {
  341. switch (width) {
  342. case 8:
  343. outb(value, port);
  344. break;
  345. case 16:
  346. outw(value, port);
  347. break;
  348. case 32:
  349. outl(value, port);
  350. break;
  351. default:
  352. BUG();
  353. }
  354. return AE_OK;
  355. }
  356. EXPORT_SYMBOL(acpi_os_write_port);
  357. acpi_status
  358. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  359. {
  360. u32 dummy;
  361. void __iomem *virt_addr;
  362. virt_addr = ioremap(phys_addr, width);
  363. if (!value)
  364. value = &dummy;
  365. switch (width) {
  366. case 8:
  367. *(u8 *) value = readb(virt_addr);
  368. break;
  369. case 16:
  370. *(u16 *) value = readw(virt_addr);
  371. break;
  372. case 32:
  373. *(u32 *) value = readl(virt_addr);
  374. break;
  375. default:
  376. BUG();
  377. }
  378. iounmap(virt_addr);
  379. return AE_OK;
  380. }
  381. acpi_status
  382. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  383. {
  384. void __iomem *virt_addr;
  385. virt_addr = ioremap(phys_addr, width);
  386. switch (width) {
  387. case 8:
  388. writeb(value, virt_addr);
  389. break;
  390. case 16:
  391. writew(value, virt_addr);
  392. break;
  393. case 32:
  394. writel(value, virt_addr);
  395. break;
  396. default:
  397. BUG();
  398. }
  399. iounmap(virt_addr);
  400. return AE_OK;
  401. }
  402. acpi_status
  403. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  404. void *value, u32 width)
  405. {
  406. int result, size;
  407. if (!value)
  408. return AE_BAD_PARAMETER;
  409. switch (width) {
  410. case 8:
  411. size = 1;
  412. break;
  413. case 16:
  414. size = 2;
  415. break;
  416. case 32:
  417. size = 4;
  418. break;
  419. default:
  420. return AE_ERROR;
  421. }
  422. BUG_ON(!raw_pci_ops);
  423. result = raw_pci_ops->read(pci_id->segment, pci_id->bus,
  424. PCI_DEVFN(pci_id->device, pci_id->function),
  425. reg, size, value);
  426. return (result ? AE_ERROR : AE_OK);
  427. }
  428. EXPORT_SYMBOL(acpi_os_read_pci_configuration);
  429. acpi_status
  430. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  431. acpi_integer value, u32 width)
  432. {
  433. int result, size;
  434. switch (width) {
  435. case 8:
  436. size = 1;
  437. break;
  438. case 16:
  439. size = 2;
  440. break;
  441. case 32:
  442. size = 4;
  443. break;
  444. default:
  445. return AE_ERROR;
  446. }
  447. BUG_ON(!raw_pci_ops);
  448. result = raw_pci_ops->write(pci_id->segment, pci_id->bus,
  449. PCI_DEVFN(pci_id->device, pci_id->function),
  450. reg, size, value);
  451. return (result ? AE_ERROR : AE_OK);
  452. }
  453. /* TODO: Change code to take advantage of driver model more */
  454. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  455. acpi_handle chandle, /* current node */
  456. struct acpi_pci_id **id,
  457. int *is_bridge, u8 * bus_number)
  458. {
  459. acpi_handle handle;
  460. struct acpi_pci_id *pci_id = *id;
  461. acpi_status status;
  462. unsigned long temp;
  463. acpi_object_type type;
  464. u8 tu8;
  465. acpi_get_parent(chandle, &handle);
  466. if (handle != rhandle) {
  467. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  468. bus_number);
  469. status = acpi_get_type(handle, &type);
  470. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  471. return;
  472. status =
  473. acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  474. &temp);
  475. if (ACPI_SUCCESS(status)) {
  476. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  477. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  478. if (*is_bridge)
  479. pci_id->bus = *bus_number;
  480. /* any nicer way to get bus number of bridge ? */
  481. status =
  482. acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8,
  483. 8);
  484. if (ACPI_SUCCESS(status)
  485. && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) {
  486. status =
  487. acpi_os_read_pci_configuration(pci_id, 0x18,
  488. &tu8, 8);
  489. if (!ACPI_SUCCESS(status)) {
  490. /* Certainly broken... FIX ME */
  491. return;
  492. }
  493. *is_bridge = 1;
  494. pci_id->bus = tu8;
  495. status =
  496. acpi_os_read_pci_configuration(pci_id, 0x19,
  497. &tu8, 8);
  498. if (ACPI_SUCCESS(status)) {
  499. *bus_number = tu8;
  500. }
  501. } else
  502. *is_bridge = 0;
  503. }
  504. }
  505. }
  506. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  507. acpi_handle chandle, /* current node */
  508. struct acpi_pci_id **id)
  509. {
  510. int is_bridge = 1;
  511. u8 bus_number = (*id)->bus;
  512. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  513. }
  514. static void acpi_os_execute_deferred(struct work_struct *work)
  515. {
  516. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  517. if (!dpc) {
  518. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  519. return;
  520. }
  521. dpc->function(dpc->context);
  522. kfree(dpc);
  523. /* Yield cpu to notify thread */
  524. cond_resched();
  525. return;
  526. }
  527. static void acpi_os_execute_notify(struct work_struct *work)
  528. {
  529. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  530. if (!dpc) {
  531. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  532. return;
  533. }
  534. dpc->function(dpc->context);
  535. kfree(dpc);
  536. return;
  537. }
  538. /*******************************************************************************
  539. *
  540. * FUNCTION: acpi_os_execute
  541. *
  542. * PARAMETERS: Type - Type of the callback
  543. * Function - Function to be executed
  544. * Context - Function parameters
  545. *
  546. * RETURN: Status
  547. *
  548. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  549. * immediately executes function on a separate thread.
  550. *
  551. ******************************************************************************/
  552. acpi_status acpi_os_execute(acpi_execute_type type,
  553. acpi_osd_exec_callback function, void *context)
  554. {
  555. acpi_status status = AE_OK;
  556. struct acpi_os_dpc *dpc;
  557. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  558. "Scheduling function [%p(%p)] for deferred execution.\n",
  559. function, context));
  560. if (!function)
  561. return AE_BAD_PARAMETER;
  562. /*
  563. * Allocate/initialize DPC structure. Note that this memory will be
  564. * freed by the callee. The kernel handles the work_struct list in a
  565. * way that allows us to also free its memory inside the callee.
  566. * Because we may want to schedule several tasks with different
  567. * parameters we can't use the approach some kernel code uses of
  568. * having a static work_struct.
  569. */
  570. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  571. if (!dpc)
  572. return_ACPI_STATUS(AE_NO_MEMORY);
  573. dpc->function = function;
  574. dpc->context = context;
  575. if (type == OSL_NOTIFY_HANDLER) {
  576. INIT_WORK(&dpc->work, acpi_os_execute_notify);
  577. if (!queue_work(kacpi_notify_wq, &dpc->work)) {
  578. status = AE_ERROR;
  579. kfree(dpc);
  580. }
  581. } else {
  582. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  583. if (!queue_work(kacpid_wq, &dpc->work)) {
  584. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  585. "Call to queue_work() failed.\n"));
  586. status = AE_ERROR;
  587. kfree(dpc);
  588. }
  589. }
  590. return_ACPI_STATUS(status);
  591. }
  592. EXPORT_SYMBOL(acpi_os_execute);
  593. void acpi_os_wait_events_complete(void *context)
  594. {
  595. flush_workqueue(kacpid_wq);
  596. }
  597. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  598. /*
  599. * Allocate the memory for a spinlock and initialize it.
  600. */
  601. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  602. {
  603. spin_lock_init(*handle);
  604. return AE_OK;
  605. }
  606. /*
  607. * Deallocate the memory for a spinlock.
  608. */
  609. void acpi_os_delete_lock(acpi_spinlock handle)
  610. {
  611. return;
  612. }
  613. acpi_status
  614. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  615. {
  616. struct semaphore *sem = NULL;
  617. sem = acpi_os_allocate(sizeof(struct semaphore));
  618. if (!sem)
  619. return AE_NO_MEMORY;
  620. memset(sem, 0, sizeof(struct semaphore));
  621. sema_init(sem, initial_units);
  622. *handle = (acpi_handle *) sem;
  623. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  624. *handle, initial_units));
  625. return AE_OK;
  626. }
  627. EXPORT_SYMBOL(acpi_os_create_semaphore);
  628. /*
  629. * TODO: A better way to delete semaphores? Linux doesn't have a
  630. * 'delete_semaphore()' function -- may result in an invalid
  631. * pointer dereference for non-synchronized consumers. Should
  632. * we at least check for blocked threads and signal/cancel them?
  633. */
  634. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  635. {
  636. struct semaphore *sem = (struct semaphore *)handle;
  637. if (!sem)
  638. return AE_BAD_PARAMETER;
  639. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  640. kfree(sem);
  641. sem = NULL;
  642. return AE_OK;
  643. }
  644. EXPORT_SYMBOL(acpi_os_delete_semaphore);
  645. /*
  646. * TODO: The kernel doesn't have a 'down_timeout' function -- had to
  647. * improvise. The process is to sleep for one scheduler quantum
  648. * until the semaphore becomes available. Downside is that this
  649. * may result in starvation for timeout-based waits when there's
  650. * lots of semaphore activity.
  651. *
  652. * TODO: Support for units > 1?
  653. */
  654. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  655. {
  656. acpi_status status = AE_OK;
  657. struct semaphore *sem = (struct semaphore *)handle;
  658. int ret = 0;
  659. if (!sem || (units < 1))
  660. return AE_BAD_PARAMETER;
  661. if (units > 1)
  662. return AE_SUPPORT;
  663. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  664. handle, units, timeout));
  665. /*
  666. * This can be called during resume with interrupts off.
  667. * Like boot-time, we should be single threaded and will
  668. * always get the lock if we try -- timeout or not.
  669. * If this doesn't succeed, then we will oops courtesy of
  670. * might_sleep() in down().
  671. */
  672. if (!down_trylock(sem))
  673. return AE_OK;
  674. switch (timeout) {
  675. /*
  676. * No Wait:
  677. * --------
  678. * A zero timeout value indicates that we shouldn't wait - just
  679. * acquire the semaphore if available otherwise return AE_TIME
  680. * (a.k.a. 'would block').
  681. */
  682. case 0:
  683. if (down_trylock(sem))
  684. status = AE_TIME;
  685. break;
  686. /*
  687. * Wait Indefinitely:
  688. * ------------------
  689. */
  690. case ACPI_WAIT_FOREVER:
  691. down(sem);
  692. break;
  693. /*
  694. * Wait w/ Timeout:
  695. * ----------------
  696. */
  697. default:
  698. // TODO: A better timeout algorithm?
  699. {
  700. int i = 0;
  701. static const int quantum_ms = 1000 / HZ;
  702. ret = down_trylock(sem);
  703. for (i = timeout; (i > 0 && ret != 0); i -= quantum_ms) {
  704. schedule_timeout_interruptible(1);
  705. ret = down_trylock(sem);
  706. }
  707. if (ret != 0)
  708. status = AE_TIME;
  709. }
  710. break;
  711. }
  712. if (ACPI_FAILURE(status)) {
  713. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  714. "Failed to acquire semaphore[%p|%d|%d], %s",
  715. handle, units, timeout,
  716. acpi_format_exception(status)));
  717. } else {
  718. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  719. "Acquired semaphore[%p|%d|%d]", handle,
  720. units, timeout));
  721. }
  722. return status;
  723. }
  724. EXPORT_SYMBOL(acpi_os_wait_semaphore);
  725. /*
  726. * TODO: Support for units > 1?
  727. */
  728. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  729. {
  730. struct semaphore *sem = (struct semaphore *)handle;
  731. if (!sem || (units < 1))
  732. return AE_BAD_PARAMETER;
  733. if (units > 1)
  734. return AE_SUPPORT;
  735. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  736. units));
  737. up(sem);
  738. return AE_OK;
  739. }
  740. EXPORT_SYMBOL(acpi_os_signal_semaphore);
  741. #ifdef ACPI_FUTURE_USAGE
  742. u32 acpi_os_get_line(char *buffer)
  743. {
  744. #ifdef ENABLE_DEBUGGER
  745. if (acpi_in_debugger) {
  746. u32 chars;
  747. kdb_read(buffer, sizeof(line_buf));
  748. /* remove the CR kdb includes */
  749. chars = strlen(buffer) - 1;
  750. buffer[chars] = '\0';
  751. }
  752. #endif
  753. return 0;
  754. }
  755. #endif /* ACPI_FUTURE_USAGE */
  756. acpi_status acpi_os_signal(u32 function, void *info)
  757. {
  758. switch (function) {
  759. case ACPI_SIGNAL_FATAL:
  760. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  761. break;
  762. case ACPI_SIGNAL_BREAKPOINT:
  763. /*
  764. * AML Breakpoint
  765. * ACPI spec. says to treat it as a NOP unless
  766. * you are debugging. So if/when we integrate
  767. * AML debugger into the kernel debugger its
  768. * hook will go here. But until then it is
  769. * not useful to print anything on breakpoints.
  770. */
  771. break;
  772. default:
  773. break;
  774. }
  775. return AE_OK;
  776. }
  777. EXPORT_SYMBOL(acpi_os_signal);
  778. static int __init acpi_os_name_setup(char *str)
  779. {
  780. char *p = acpi_os_name;
  781. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  782. if (!str || !*str)
  783. return 0;
  784. for (; count-- && str && *str; str++) {
  785. if (isalnum(*str) || *str == ' ' || *str == ':')
  786. *p++ = *str;
  787. else if (*str == '\'' || *str == '"')
  788. continue;
  789. else
  790. break;
  791. }
  792. *p = 0;
  793. return 1;
  794. }
  795. __setup("acpi_os_name=", acpi_os_name_setup);
  796. static void enable_osi_linux(int enable) {
  797. if (osi_linux != enable)
  798. printk(KERN_INFO PREFIX "%sabled _OSI(Linux)\n",
  799. enable ? "En": "Dis");
  800. osi_linux = enable;
  801. return;
  802. }
  803. /*
  804. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  805. *
  806. * empty string disables _OSI
  807. * string starting with '!' disables that string
  808. * otherwise string is added to list, augmenting built-in strings
  809. */
  810. static int __init acpi_osi_setup(char *str)
  811. {
  812. if (str == NULL || *str == '\0') {
  813. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  814. acpi_gbl_create_osi_method = FALSE;
  815. } else if (!strcmp("!Linux", str)) {
  816. enable_osi_linux(0);
  817. } else if (*str == '!') {
  818. if (acpi_osi_invalidate(++str) == AE_OK)
  819. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  820. } else if (!strcmp("Linux", str)) {
  821. enable_osi_linux(1);
  822. } else if (*osi_additional_string == '\0') {
  823. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  824. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  825. }
  826. return 1;
  827. }
  828. __setup("acpi_osi=", acpi_osi_setup);
  829. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  830. static int __init acpi_serialize_setup(char *str)
  831. {
  832. printk(KERN_INFO PREFIX "serialize enabled\n");
  833. acpi_gbl_all_methods_serialized = TRUE;
  834. return 1;
  835. }
  836. __setup("acpi_serialize", acpi_serialize_setup);
  837. /*
  838. * Wake and Run-Time GPES are expected to be separate.
  839. * We disable wake-GPEs at run-time to prevent spurious
  840. * interrupts.
  841. *
  842. * However, if a system exists that shares Wake and
  843. * Run-time events on the same GPE this flag is available
  844. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  845. */
  846. static int __init acpi_wake_gpes_always_on_setup(char *str)
  847. {
  848. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  849. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  850. return 1;
  851. }
  852. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  853. /*
  854. * Acquire a spinlock.
  855. *
  856. * handle is a pointer to the spinlock_t.
  857. */
  858. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  859. {
  860. acpi_cpu_flags flags;
  861. spin_lock_irqsave(lockp, flags);
  862. return flags;
  863. }
  864. /*
  865. * Release a spinlock. See above.
  866. */
  867. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  868. {
  869. spin_unlock_irqrestore(lockp, flags);
  870. }
  871. #ifndef ACPI_USE_LOCAL_CACHE
  872. /*******************************************************************************
  873. *
  874. * FUNCTION: acpi_os_create_cache
  875. *
  876. * PARAMETERS: name - Ascii name for the cache
  877. * size - Size of each cached object
  878. * depth - Maximum depth of the cache (in objects) <ignored>
  879. * cache - Where the new cache object is returned
  880. *
  881. * RETURN: status
  882. *
  883. * DESCRIPTION: Create a cache object
  884. *
  885. ******************************************************************************/
  886. acpi_status
  887. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  888. {
  889. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  890. if (*cache == NULL)
  891. return AE_ERROR;
  892. else
  893. return AE_OK;
  894. }
  895. /*******************************************************************************
  896. *
  897. * FUNCTION: acpi_os_purge_cache
  898. *
  899. * PARAMETERS: Cache - Handle to cache object
  900. *
  901. * RETURN: Status
  902. *
  903. * DESCRIPTION: Free all objects within the requested cache.
  904. *
  905. ******************************************************************************/
  906. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  907. {
  908. kmem_cache_shrink(cache);
  909. return (AE_OK);
  910. }
  911. /*******************************************************************************
  912. *
  913. * FUNCTION: acpi_os_delete_cache
  914. *
  915. * PARAMETERS: Cache - Handle to cache object
  916. *
  917. * RETURN: Status
  918. *
  919. * DESCRIPTION: Free all objects within the requested cache and delete the
  920. * cache object.
  921. *
  922. ******************************************************************************/
  923. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  924. {
  925. kmem_cache_destroy(cache);
  926. return (AE_OK);
  927. }
  928. /*******************************************************************************
  929. *
  930. * FUNCTION: acpi_os_release_object
  931. *
  932. * PARAMETERS: Cache - Handle to cache object
  933. * Object - The object to be released
  934. *
  935. * RETURN: None
  936. *
  937. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  938. * the object is deleted.
  939. *
  940. ******************************************************************************/
  941. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  942. {
  943. kmem_cache_free(cache, object);
  944. return (AE_OK);
  945. }
  946. /******************************************************************************
  947. *
  948. * FUNCTION: acpi_os_validate_interface
  949. *
  950. * PARAMETERS: interface - Requested interface to be validated
  951. *
  952. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  953. *
  954. * DESCRIPTION: Match an interface string to the interfaces supported by the
  955. * host. Strings originate from an AML call to the _OSI method.
  956. *
  957. *****************************************************************************/
  958. acpi_status
  959. acpi_os_validate_interface (char *interface)
  960. {
  961. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  962. return AE_OK;
  963. if (!strcmp("Linux", interface)) {
  964. printk(KERN_WARNING PREFIX
  965. "System BIOS is requesting _OSI(Linux)\n");
  966. printk(KERN_WARNING PREFIX
  967. "If \"acpi_osi=Linux\" works better,\n"
  968. "Please send dmidecode "
  969. "to linux-acpi@vger.kernel.org\n");
  970. if(osi_linux)
  971. return AE_OK;
  972. }
  973. return AE_SUPPORT;
  974. }
  975. /******************************************************************************
  976. *
  977. * FUNCTION: acpi_os_validate_address
  978. *
  979. * PARAMETERS: space_id - ACPI space ID
  980. * address - Physical address
  981. * length - Address length
  982. *
  983. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  984. * should return AE_AML_ILLEGAL_ADDRESS.
  985. *
  986. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  987. * the addresses accessed by AML operation regions.
  988. *
  989. *****************************************************************************/
  990. acpi_status
  991. acpi_os_validate_address (
  992. u8 space_id,
  993. acpi_physical_address address,
  994. acpi_size length)
  995. {
  996. return AE_OK;
  997. }
  998. #ifdef CONFIG_DMI
  999. static int dmi_osi_linux(const struct dmi_system_id *d)
  1000. {
  1001. printk(KERN_NOTICE "%s detected: enabling _OSI(Linux)\n", d->ident);
  1002. enable_osi_linux(1);
  1003. return 0;
  1004. }
  1005. static struct dmi_system_id acpi_osl_dmi_table[] __initdata = {
  1006. /*
  1007. * Boxes that need _OSI(Linux)
  1008. */
  1009. {
  1010. .callback = dmi_osi_linux,
  1011. .ident = "Intel Napa CRB",
  1012. .matches = {
  1013. DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
  1014. DMI_MATCH(DMI_BOARD_NAME, "MPAD-MSAE Customer Reference Boards"),
  1015. },
  1016. },
  1017. {}
  1018. };
  1019. #endif /* CONFIG_DMI */
  1020. #endif