osl.c 27 KB

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