osl.c 25 KB

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