osl.c 26 KB

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