osl.c 25 KB

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