osl.c 26 KB

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