osl.c 25 KB

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