osl.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. * Copyright (c) 2008 Intel Corporation
  8. * Author: Matthew Wilcox <willy@linux.intel.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/mm.h>
  33. #include <linux/pci.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/acpi.h>
  40. #include <linux/efi.h>
  41. #include <linux/ioport.h>
  42. #include <linux/list.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/semaphore.h>
  45. #include <asm/io.h>
  46. #include <asm/uaccess.h>
  47. #include <acpi/acpi.h>
  48. #include <acpi/acpi_bus.h>
  49. #include <acpi/processor.h>
  50. #define _COMPONENT ACPI_OS_SERVICES
  51. ACPI_MODULE_NAME("osl");
  52. #define PREFIX "ACPI: "
  53. struct acpi_os_dpc {
  54. acpi_osd_exec_callback function;
  55. void *context;
  56. struct work_struct work;
  57. int wait;
  58. };
  59. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  60. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  61. #endif
  62. #ifdef ENABLE_DEBUGGER
  63. #include <linux/kdb.h>
  64. /* stuff for debugger support */
  65. int acpi_in_debugger;
  66. EXPORT_SYMBOL(acpi_in_debugger);
  67. extern char line_buf[80];
  68. #endif /*ENABLE_DEBUGGER */
  69. static unsigned int acpi_irq_irq;
  70. static acpi_osd_handler acpi_irq_handler;
  71. static void *acpi_irq_context;
  72. static struct workqueue_struct *kacpid_wq;
  73. static struct workqueue_struct *kacpi_notify_wq;
  74. static struct workqueue_struct *kacpi_hotplug_wq;
  75. struct acpi_res_list {
  76. resource_size_t start;
  77. resource_size_t end;
  78. acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
  79. char name[5]; /* only can have a length of 4 chars, make use of this
  80. one instead of res->name, no need to kalloc then */
  81. struct list_head resource_list;
  82. int count;
  83. };
  84. static LIST_HEAD(resource_list_head);
  85. static DEFINE_SPINLOCK(acpi_res_lock);
  86. #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
  87. static char osi_additional_string[OSI_STRING_LENGTH_MAX];
  88. /*
  89. * The story of _OSI(Linux)
  90. *
  91. * From pre-history through Linux-2.6.22,
  92. * Linux responded TRUE upon a BIOS OSI(Linux) query.
  93. *
  94. * Unfortunately, reference BIOS writers got wind of this
  95. * and put OSI(Linux) in their example code, quickly exposing
  96. * this string as ill-conceived and opening the door to
  97. * an un-bounded number of BIOS incompatibilities.
  98. *
  99. * For example, OSI(Linux) was used on resume to re-POST a
  100. * video card on one system, because Linux at that time
  101. * could not do a speedy restore in its native driver.
  102. * But then upon gaining quick native restore capability,
  103. * Linux has no way to tell the BIOS to skip the time-consuming
  104. * POST -- putting Linux at a permanent performance disadvantage.
  105. * On another system, the BIOS writer used OSI(Linux)
  106. * to infer native OS support for IPMI! On other systems,
  107. * OSI(Linux) simply got in the way of Linux claiming to
  108. * be compatible with other operating systems, exposing
  109. * BIOS issues such as skipped device initialization.
  110. *
  111. * So "Linux" turned out to be a really poor chose of
  112. * OSI string, and from Linux-2.6.23 onward we respond FALSE.
  113. *
  114. * BIOS writers should NOT query _OSI(Linux) on future systems.
  115. * Linux will complain on the console when it sees it, and return FALSE.
  116. * To get Linux to return TRUE for your system will require
  117. * a kernel source update to add a DMI entry,
  118. * or boot with "acpi_osi=Linux"
  119. */
  120. static struct osi_linux {
  121. unsigned int enable:1;
  122. unsigned int dmi:1;
  123. unsigned int cmdline:1;
  124. unsigned int known:1;
  125. } osi_linux = { 0, 0, 0, 0};
  126. static void __init acpi_request_region (struct acpi_generic_address *addr,
  127. unsigned int length, char *desc)
  128. {
  129. struct resource *res;
  130. if (!addr->address || !length)
  131. return;
  132. if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  133. res = request_region(addr->address, length, desc);
  134. else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  135. res = request_mem_region(addr->address, length, desc);
  136. }
  137. static int __init acpi_reserve_resources(void)
  138. {
  139. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  140. "ACPI PM1a_EVT_BLK");
  141. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  142. "ACPI PM1b_EVT_BLK");
  143. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  144. "ACPI PM1a_CNT_BLK");
  145. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  146. "ACPI PM1b_CNT_BLK");
  147. if (acpi_gbl_FADT.pm_timer_length == 4)
  148. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  149. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  150. "ACPI PM2_CNT_BLK");
  151. /* Length of GPE blocks must be a non-negative multiple of 2 */
  152. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  153. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  154. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  155. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  156. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  157. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  158. return 0;
  159. }
  160. device_initcall(acpi_reserve_resources);
  161. acpi_status __init acpi_os_initialize(void)
  162. {
  163. return AE_OK;
  164. }
  165. acpi_status acpi_os_initialize1(void)
  166. {
  167. kacpid_wq = create_workqueue("kacpid");
  168. kacpi_notify_wq = create_workqueue("kacpi_notify");
  169. kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
  170. BUG_ON(!kacpid_wq);
  171. BUG_ON(!kacpi_notify_wq);
  172. BUG_ON(!kacpi_hotplug_wq);
  173. return AE_OK;
  174. }
  175. acpi_status acpi_os_terminate(void)
  176. {
  177. if (acpi_irq_handler) {
  178. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  179. acpi_irq_handler);
  180. }
  181. destroy_workqueue(kacpid_wq);
  182. destroy_workqueue(kacpi_notify_wq);
  183. destroy_workqueue(kacpi_hotplug_wq);
  184. return AE_OK;
  185. }
  186. void acpi_os_printf(const char *fmt, ...)
  187. {
  188. va_list args;
  189. va_start(args, fmt);
  190. acpi_os_vprintf(fmt, args);
  191. va_end(args);
  192. }
  193. void acpi_os_vprintf(const char *fmt, va_list args)
  194. {
  195. static char buffer[512];
  196. vsprintf(buffer, fmt, args);
  197. #ifdef ENABLE_DEBUGGER
  198. if (acpi_in_debugger) {
  199. kdb_printf("%s", buffer);
  200. } else {
  201. printk(KERN_CONT "%s", buffer);
  202. }
  203. #else
  204. printk(KERN_CONT "%s", buffer);
  205. #endif
  206. }
  207. acpi_physical_address __init acpi_os_get_root_pointer(void)
  208. {
  209. if (efi_enabled) {
  210. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  211. return efi.acpi20;
  212. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  213. return efi.acpi;
  214. else {
  215. printk(KERN_ERR PREFIX
  216. "System description tables not found\n");
  217. return 0;
  218. }
  219. } else {
  220. acpi_physical_address pa = 0;
  221. acpi_find_root_pointer(&pa);
  222. return pa;
  223. }
  224. }
  225. void __iomem *__init_refok
  226. acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  227. {
  228. if (phys > ULONG_MAX) {
  229. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  230. return NULL;
  231. }
  232. if (acpi_gbl_permanent_mmap)
  233. /*
  234. * ioremap checks to ensure this is in reserved space
  235. */
  236. return ioremap((unsigned long)phys, size);
  237. else
  238. return __acpi_map_table((unsigned long)phys, size);
  239. }
  240. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  241. void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  242. {
  243. if (acpi_gbl_permanent_mmap)
  244. iounmap(virt);
  245. else
  246. __acpi_unmap_table(virt, size);
  247. }
  248. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  249. void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  250. {
  251. if (!acpi_gbl_permanent_mmap)
  252. __acpi_unmap_table(virt, size);
  253. }
  254. #ifdef ACPI_FUTURE_USAGE
  255. acpi_status
  256. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  257. {
  258. if (!phys || !virt)
  259. return AE_BAD_PARAMETER;
  260. *phys = virt_to_phys(virt);
  261. return AE_OK;
  262. }
  263. #endif
  264. #define ACPI_MAX_OVERRIDE_LEN 100
  265. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  266. acpi_status
  267. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  268. acpi_string * new_val)
  269. {
  270. if (!init_val || !new_val)
  271. return AE_BAD_PARAMETER;
  272. *new_val = NULL;
  273. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  274. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  275. acpi_os_name);
  276. *new_val = acpi_os_name;
  277. }
  278. return AE_OK;
  279. }
  280. acpi_status
  281. acpi_os_table_override(struct acpi_table_header * existing_table,
  282. struct acpi_table_header ** new_table)
  283. {
  284. if (!existing_table || !new_table)
  285. return AE_BAD_PARAMETER;
  286. *new_table = NULL;
  287. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  288. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  289. *new_table = (struct acpi_table_header *)AmlCode;
  290. #endif
  291. if (*new_table != NULL) {
  292. printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
  293. "this is unsafe: tainting kernel\n",
  294. existing_table->signature,
  295. existing_table->oem_table_id);
  296. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
  297. }
  298. return AE_OK;
  299. }
  300. static irqreturn_t acpi_irq(int irq, void *dev_id)
  301. {
  302. u32 handled;
  303. handled = (*acpi_irq_handler) (acpi_irq_context);
  304. if (handled) {
  305. acpi_irq_handled++;
  306. return IRQ_HANDLED;
  307. } else {
  308. acpi_irq_not_handled++;
  309. return IRQ_NONE;
  310. }
  311. }
  312. acpi_status
  313. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  314. void *context)
  315. {
  316. unsigned int irq;
  317. acpi_irq_stats_init();
  318. /*
  319. * Ignore the GSI from the core, and use the value in our copy of the
  320. * FADT. It may not be the same if an interrupt source override exists
  321. * for the SCI.
  322. */
  323. gsi = acpi_gbl_FADT.sci_interrupt;
  324. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  325. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  326. gsi);
  327. return AE_OK;
  328. }
  329. acpi_irq_handler = handler;
  330. acpi_irq_context = context;
  331. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  332. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  333. return AE_NOT_ACQUIRED;
  334. }
  335. acpi_irq_irq = irq;
  336. return AE_OK;
  337. }
  338. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  339. {
  340. if (irq) {
  341. free_irq(irq, acpi_irq);
  342. acpi_irq_handler = NULL;
  343. acpi_irq_irq = 0;
  344. }
  345. return AE_OK;
  346. }
  347. /*
  348. * Running in interpreter thread context, safe to sleep
  349. */
  350. void acpi_os_sleep(u64 ms)
  351. {
  352. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  353. }
  354. void acpi_os_stall(u32 us)
  355. {
  356. while (us) {
  357. u32 delay = 1000;
  358. if (delay > us)
  359. delay = us;
  360. udelay(delay);
  361. touch_nmi_watchdog();
  362. us -= delay;
  363. }
  364. }
  365. /*
  366. * Support ACPI 3.0 AML Timer operand
  367. * Returns 64-bit free-running, monotonically increasing timer
  368. * with 100ns granularity
  369. */
  370. u64 acpi_os_get_timer(void)
  371. {
  372. static u64 t;
  373. #ifdef CONFIG_HPET
  374. /* TBD: use HPET if available */
  375. #endif
  376. #ifdef CONFIG_X86_PM_TIMER
  377. /* TBD: default to PM timer if HPET was not available */
  378. #endif
  379. if (!t)
  380. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  381. return ++t;
  382. }
  383. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  384. {
  385. u32 dummy;
  386. if (!value)
  387. value = &dummy;
  388. *value = 0;
  389. if (width <= 8) {
  390. *(u8 *) value = inb(port);
  391. } else if (width <= 16) {
  392. *(u16 *) value = inw(port);
  393. } else if (width <= 32) {
  394. *(u32 *) value = inl(port);
  395. } else {
  396. BUG();
  397. }
  398. return AE_OK;
  399. }
  400. EXPORT_SYMBOL(acpi_os_read_port);
  401. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  402. {
  403. if (width <= 8) {
  404. outb(value, port);
  405. } else if (width <= 16) {
  406. outw(value, port);
  407. } else if (width <= 32) {
  408. outl(value, port);
  409. } else {
  410. BUG();
  411. }
  412. return AE_OK;
  413. }
  414. EXPORT_SYMBOL(acpi_os_write_port);
  415. acpi_status
  416. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  417. {
  418. u32 dummy;
  419. void __iomem *virt_addr;
  420. virt_addr = ioremap(phys_addr, width);
  421. if (!value)
  422. value = &dummy;
  423. switch (width) {
  424. case 8:
  425. *(u8 *) value = readb(virt_addr);
  426. break;
  427. case 16:
  428. *(u16 *) value = readw(virt_addr);
  429. break;
  430. case 32:
  431. *(u32 *) value = readl(virt_addr);
  432. break;
  433. default:
  434. BUG();
  435. }
  436. iounmap(virt_addr);
  437. return AE_OK;
  438. }
  439. acpi_status
  440. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  441. {
  442. void __iomem *virt_addr;
  443. virt_addr = ioremap(phys_addr, width);
  444. switch (width) {
  445. case 8:
  446. writeb(value, virt_addr);
  447. break;
  448. case 16:
  449. writew(value, virt_addr);
  450. break;
  451. case 32:
  452. writel(value, virt_addr);
  453. break;
  454. default:
  455. BUG();
  456. }
  457. iounmap(virt_addr);
  458. return AE_OK;
  459. }
  460. acpi_status
  461. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  462. u32 *value, u32 width)
  463. {
  464. int result, size;
  465. if (!value)
  466. return AE_BAD_PARAMETER;
  467. switch (width) {
  468. case 8:
  469. size = 1;
  470. break;
  471. case 16:
  472. size = 2;
  473. break;
  474. case 32:
  475. size = 4;
  476. break;
  477. default:
  478. return AE_ERROR;
  479. }
  480. result = raw_pci_read(pci_id->segment, pci_id->bus,
  481. PCI_DEVFN(pci_id->device, pci_id->function),
  482. reg, size, value);
  483. return (result ? AE_ERROR : AE_OK);
  484. }
  485. acpi_status
  486. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  487. u64 value, u32 width)
  488. {
  489. int result, size;
  490. switch (width) {
  491. case 8:
  492. size = 1;
  493. break;
  494. case 16:
  495. size = 2;
  496. break;
  497. case 32:
  498. size = 4;
  499. break;
  500. default:
  501. return AE_ERROR;
  502. }
  503. result = raw_pci_write(pci_id->segment, pci_id->bus,
  504. PCI_DEVFN(pci_id->device, pci_id->function),
  505. reg, size, value);
  506. return (result ? AE_ERROR : AE_OK);
  507. }
  508. /* TODO: Change code to take advantage of driver model more */
  509. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  510. acpi_handle chandle, /* current node */
  511. struct acpi_pci_id **id,
  512. int *is_bridge, u8 * bus_number)
  513. {
  514. acpi_handle handle;
  515. struct acpi_pci_id *pci_id = *id;
  516. acpi_status status;
  517. unsigned long long temp;
  518. acpi_object_type type;
  519. acpi_get_parent(chandle, &handle);
  520. if (handle != rhandle) {
  521. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  522. bus_number);
  523. status = acpi_get_type(handle, &type);
  524. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  525. return;
  526. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  527. &temp);
  528. if (ACPI_SUCCESS(status)) {
  529. u32 val;
  530. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  531. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  532. if (*is_bridge)
  533. pci_id->bus = *bus_number;
  534. /* any nicer way to get bus number of bridge ? */
  535. status =
  536. acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
  537. 8);
  538. if (ACPI_SUCCESS(status)
  539. && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
  540. status =
  541. acpi_os_read_pci_configuration(pci_id, 0x18,
  542. &val, 8);
  543. if (!ACPI_SUCCESS(status)) {
  544. /* Certainly broken... FIX ME */
  545. return;
  546. }
  547. *is_bridge = 1;
  548. pci_id->bus = val;
  549. status =
  550. acpi_os_read_pci_configuration(pci_id, 0x19,
  551. &val, 8);
  552. if (ACPI_SUCCESS(status)) {
  553. *bus_number = val;
  554. }
  555. } else
  556. *is_bridge = 0;
  557. }
  558. }
  559. }
  560. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  561. acpi_handle chandle, /* current node */
  562. struct acpi_pci_id **id)
  563. {
  564. int is_bridge = 1;
  565. u8 bus_number = (*id)->bus;
  566. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  567. }
  568. static void acpi_os_execute_deferred(struct work_struct *work)
  569. {
  570. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  571. if (dpc->wait)
  572. acpi_os_wait_events_complete(NULL);
  573. dpc->function(dpc->context);
  574. kfree(dpc);
  575. }
  576. /*******************************************************************************
  577. *
  578. * FUNCTION: acpi_os_execute
  579. *
  580. * PARAMETERS: Type - Type of the callback
  581. * Function - Function to be executed
  582. * Context - Function parameters
  583. *
  584. * RETURN: Status
  585. *
  586. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  587. * immediately executes function on a separate thread.
  588. *
  589. ******************************************************************************/
  590. static acpi_status __acpi_os_execute(acpi_execute_type type,
  591. acpi_osd_exec_callback function, void *context, int hp)
  592. {
  593. acpi_status status = AE_OK;
  594. struct acpi_os_dpc *dpc;
  595. struct workqueue_struct *queue;
  596. int ret;
  597. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  598. "Scheduling function [%p(%p)] for deferred execution.\n",
  599. function, context));
  600. /*
  601. * Allocate/initialize DPC structure. Note that this memory will be
  602. * freed by the callee. The kernel handles the work_struct list in a
  603. * way that allows us to also free its memory inside the callee.
  604. * Because we may want to schedule several tasks with different
  605. * parameters we can't use the approach some kernel code uses of
  606. * having a static work_struct.
  607. */
  608. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  609. if (!dpc)
  610. return AE_NO_MEMORY;
  611. dpc->function = function;
  612. dpc->context = context;
  613. /*
  614. * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
  615. * because the hotplug code may call driver .remove() functions,
  616. * which invoke flush_scheduled_work/acpi_os_wait_events_complete
  617. * to flush these workqueues.
  618. */
  619. queue = hp ? kacpi_hotplug_wq :
  620. (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
  621. dpc->wait = hp ? 1 : 0;
  622. if (queue == kacpi_hotplug_wq)
  623. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  624. else if (queue == kacpi_notify_wq)
  625. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  626. else
  627. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  628. /*
  629. * On some machines, a software-initiated SMI causes corruption unless
  630. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  631. * typically it's done in GPE-related methods that are run via
  632. * workqueues, so we can avoid the known corruption cases by always
  633. * queueing on CPU 0.
  634. */
  635. ret = queue_work_on(0, queue, &dpc->work);
  636. if (!ret) {
  637. printk(KERN_ERR PREFIX
  638. "Call to queue_work() failed.\n");
  639. status = AE_ERROR;
  640. kfree(dpc);
  641. }
  642. return status;
  643. }
  644. acpi_status acpi_os_execute(acpi_execute_type type,
  645. acpi_osd_exec_callback function, void *context)
  646. {
  647. return __acpi_os_execute(type, function, context, 0);
  648. }
  649. EXPORT_SYMBOL(acpi_os_execute);
  650. acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
  651. void *context)
  652. {
  653. return __acpi_os_execute(0, function, context, 1);
  654. }
  655. void acpi_os_wait_events_complete(void *context)
  656. {
  657. flush_workqueue(kacpid_wq);
  658. flush_workqueue(kacpi_notify_wq);
  659. }
  660. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  661. /*
  662. * Allocate the memory for a spinlock and initialize it.
  663. */
  664. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  665. {
  666. spin_lock_init(*handle);
  667. return AE_OK;
  668. }
  669. /*
  670. * Deallocate the memory for a spinlock.
  671. */
  672. void acpi_os_delete_lock(acpi_spinlock handle)
  673. {
  674. return;
  675. }
  676. acpi_status
  677. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  678. {
  679. struct semaphore *sem = NULL;
  680. sem = acpi_os_allocate(sizeof(struct semaphore));
  681. if (!sem)
  682. return AE_NO_MEMORY;
  683. memset(sem, 0, sizeof(struct semaphore));
  684. sema_init(sem, initial_units);
  685. *handle = (acpi_handle *) sem;
  686. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  687. *handle, initial_units));
  688. return AE_OK;
  689. }
  690. /*
  691. * TODO: A better way to delete semaphores? Linux doesn't have a
  692. * 'delete_semaphore()' function -- may result in an invalid
  693. * pointer dereference for non-synchronized consumers. Should
  694. * we at least check for blocked threads and signal/cancel them?
  695. */
  696. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  697. {
  698. struct semaphore *sem = (struct semaphore *)handle;
  699. if (!sem)
  700. return AE_BAD_PARAMETER;
  701. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  702. BUG_ON(!list_empty(&sem->wait_list));
  703. kfree(sem);
  704. sem = NULL;
  705. return AE_OK;
  706. }
  707. /*
  708. * TODO: Support for units > 1?
  709. */
  710. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  711. {
  712. acpi_status status = AE_OK;
  713. struct semaphore *sem = (struct semaphore *)handle;
  714. long jiffies;
  715. int ret = 0;
  716. if (!sem || (units < 1))
  717. return AE_BAD_PARAMETER;
  718. if (units > 1)
  719. return AE_SUPPORT;
  720. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  721. handle, units, timeout));
  722. if (timeout == ACPI_WAIT_FOREVER)
  723. jiffies = MAX_SCHEDULE_TIMEOUT;
  724. else
  725. jiffies = msecs_to_jiffies(timeout);
  726. ret = down_timeout(sem, jiffies);
  727. if (ret)
  728. status = AE_TIME;
  729. if (ACPI_FAILURE(status)) {
  730. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  731. "Failed to acquire semaphore[%p|%d|%d], %s",
  732. handle, units, timeout,
  733. acpi_format_exception(status)));
  734. } else {
  735. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  736. "Acquired semaphore[%p|%d|%d]", handle,
  737. units, timeout));
  738. }
  739. return status;
  740. }
  741. /*
  742. * TODO: Support for units > 1?
  743. */
  744. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  745. {
  746. struct semaphore *sem = (struct semaphore *)handle;
  747. if (!sem || (units < 1))
  748. return AE_BAD_PARAMETER;
  749. if (units > 1)
  750. return AE_SUPPORT;
  751. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  752. units));
  753. up(sem);
  754. return AE_OK;
  755. }
  756. #ifdef ACPI_FUTURE_USAGE
  757. u32 acpi_os_get_line(char *buffer)
  758. {
  759. #ifdef ENABLE_DEBUGGER
  760. if (acpi_in_debugger) {
  761. u32 chars;
  762. kdb_read(buffer, sizeof(line_buf));
  763. /* remove the CR kdb includes */
  764. chars = strlen(buffer) - 1;
  765. buffer[chars] = '\0';
  766. }
  767. #endif
  768. return 0;
  769. }
  770. #endif /* ACPI_FUTURE_USAGE */
  771. acpi_status acpi_os_signal(u32 function, void *info)
  772. {
  773. switch (function) {
  774. case ACPI_SIGNAL_FATAL:
  775. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  776. break;
  777. case ACPI_SIGNAL_BREAKPOINT:
  778. /*
  779. * AML Breakpoint
  780. * ACPI spec. says to treat it as a NOP unless
  781. * you are debugging. So if/when we integrate
  782. * AML debugger into the kernel debugger its
  783. * hook will go here. But until then it is
  784. * not useful to print anything on breakpoints.
  785. */
  786. break;
  787. default:
  788. break;
  789. }
  790. return AE_OK;
  791. }
  792. static int __init acpi_os_name_setup(char *str)
  793. {
  794. char *p = acpi_os_name;
  795. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  796. if (!str || !*str)
  797. return 0;
  798. for (; count-- && str && *str; str++) {
  799. if (isalnum(*str) || *str == ' ' || *str == ':')
  800. *p++ = *str;
  801. else if (*str == '\'' || *str == '"')
  802. continue;
  803. else
  804. break;
  805. }
  806. *p = 0;
  807. return 1;
  808. }
  809. __setup("acpi_os_name=", acpi_os_name_setup);
  810. static void __init set_osi_linux(unsigned int enable)
  811. {
  812. if (osi_linux.enable != enable) {
  813. osi_linux.enable = enable;
  814. printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
  815. enable ? "Add": "Delet");
  816. }
  817. return;
  818. }
  819. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  820. {
  821. osi_linux.cmdline = 1; /* cmdline set the default */
  822. set_osi_linux(enable);
  823. return;
  824. }
  825. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  826. {
  827. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  828. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  829. if (enable == -1)
  830. return;
  831. osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
  832. set_osi_linux(enable);
  833. return;
  834. }
  835. /*
  836. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  837. *
  838. * empty string disables _OSI
  839. * string starting with '!' disables that string
  840. * otherwise string is added to list, augmenting built-in strings
  841. */
  842. int __init acpi_osi_setup(char *str)
  843. {
  844. if (str == NULL || *str == '\0') {
  845. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  846. acpi_gbl_create_osi_method = FALSE;
  847. } else if (!strcmp("!Linux", str)) {
  848. acpi_cmdline_osi_linux(0); /* !enable */
  849. } else if (*str == '!') {
  850. if (acpi_osi_invalidate(++str) == AE_OK)
  851. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  852. } else if (!strcmp("Linux", str)) {
  853. acpi_cmdline_osi_linux(1); /* enable */
  854. } else if (*osi_additional_string == '\0') {
  855. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  856. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  857. }
  858. return 1;
  859. }
  860. __setup("acpi_osi=", acpi_osi_setup);
  861. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  862. static int __init acpi_serialize_setup(char *str)
  863. {
  864. printk(KERN_INFO PREFIX "serialize enabled\n");
  865. acpi_gbl_all_methods_serialized = TRUE;
  866. return 1;
  867. }
  868. __setup("acpi_serialize", acpi_serialize_setup);
  869. /*
  870. * Wake and Run-Time GPES are expected to be separate.
  871. * We disable wake-GPEs at run-time to prevent spurious
  872. * interrupts.
  873. *
  874. * However, if a system exists that shares Wake and
  875. * Run-time events on the same GPE this flag is available
  876. * to tell Linux to keep the wake-time GPEs enabled at run-time.
  877. */
  878. static int __init acpi_wake_gpes_always_on_setup(char *str)
  879. {
  880. printk(KERN_INFO PREFIX "wake GPEs not disabled\n");
  881. acpi_gbl_leave_wake_gpes_disabled = FALSE;
  882. return 1;
  883. }
  884. __setup("acpi_wake_gpes_always_on", acpi_wake_gpes_always_on_setup);
  885. /* Check of resource interference between native drivers and ACPI
  886. * OperationRegions (SystemIO and System Memory only).
  887. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  888. * in arbitrary AML code and can interfere with legacy drivers.
  889. * acpi_enforce_resources= can be set to:
  890. *
  891. * - strict (default) (2)
  892. * -> further driver trying to access the resources will not load
  893. * - lax (1)
  894. * -> further driver trying to access the resources will load, but you
  895. * get a system message that something might go wrong...
  896. *
  897. * - no (0)
  898. * -> ACPI Operation Region resources will not be registered
  899. *
  900. */
  901. #define ENFORCE_RESOURCES_STRICT 2
  902. #define ENFORCE_RESOURCES_LAX 1
  903. #define ENFORCE_RESOURCES_NO 0
  904. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  905. static int __init acpi_enforce_resources_setup(char *str)
  906. {
  907. if (str == NULL || *str == '\0')
  908. return 0;
  909. if (!strcmp("strict", str))
  910. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  911. else if (!strcmp("lax", str))
  912. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  913. else if (!strcmp("no", str))
  914. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  915. return 1;
  916. }
  917. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  918. /* Check for resource conflicts between ACPI OperationRegions and native
  919. * drivers */
  920. int acpi_check_resource_conflict(const struct resource *res)
  921. {
  922. struct acpi_res_list *res_list_elem;
  923. int ioport;
  924. int clash = 0;
  925. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  926. return 0;
  927. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  928. return 0;
  929. ioport = res->flags & IORESOURCE_IO;
  930. spin_lock(&acpi_res_lock);
  931. list_for_each_entry(res_list_elem, &resource_list_head,
  932. resource_list) {
  933. if (ioport && (res_list_elem->resource_type
  934. != ACPI_ADR_SPACE_SYSTEM_IO))
  935. continue;
  936. if (!ioport && (res_list_elem->resource_type
  937. != ACPI_ADR_SPACE_SYSTEM_MEMORY))
  938. continue;
  939. if (res->end < res_list_elem->start
  940. || res_list_elem->end < res->start)
  941. continue;
  942. clash = 1;
  943. break;
  944. }
  945. spin_unlock(&acpi_res_lock);
  946. if (clash) {
  947. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  948. printk(KERN_WARNING "ACPI: resource %s %pR"
  949. " conflicts with ACPI region %s %pR\n",
  950. res->name, res, res_list_elem->name,
  951. res_list_elem);
  952. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  953. printk(KERN_NOTICE "ACPI: This conflict may"
  954. " cause random problems and system"
  955. " instability\n");
  956. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  957. " for this device, you should use it instead of"
  958. " the native driver\n");
  959. }
  960. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  961. return -EBUSY;
  962. }
  963. return 0;
  964. }
  965. EXPORT_SYMBOL(acpi_check_resource_conflict);
  966. int acpi_check_region(resource_size_t start, resource_size_t n,
  967. const char *name)
  968. {
  969. struct resource res = {
  970. .start = start,
  971. .end = start + n - 1,
  972. .name = name,
  973. .flags = IORESOURCE_IO,
  974. };
  975. return acpi_check_resource_conflict(&res);
  976. }
  977. EXPORT_SYMBOL(acpi_check_region);
  978. int acpi_check_mem_region(resource_size_t start, resource_size_t n,
  979. const char *name)
  980. {
  981. struct resource res = {
  982. .start = start,
  983. .end = start + n - 1,
  984. .name = name,
  985. .flags = IORESOURCE_MEM,
  986. };
  987. return acpi_check_resource_conflict(&res);
  988. }
  989. EXPORT_SYMBOL(acpi_check_mem_region);
  990. /*
  991. * Let drivers know whether the resource checks are effective
  992. */
  993. int acpi_resources_are_enforced(void)
  994. {
  995. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  996. }
  997. EXPORT_SYMBOL(acpi_resources_are_enforced);
  998. /*
  999. * Acquire a spinlock.
  1000. *
  1001. * handle is a pointer to the spinlock_t.
  1002. */
  1003. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1004. {
  1005. acpi_cpu_flags flags;
  1006. spin_lock_irqsave(lockp, flags);
  1007. return flags;
  1008. }
  1009. /*
  1010. * Release a spinlock. See above.
  1011. */
  1012. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1013. {
  1014. spin_unlock_irqrestore(lockp, flags);
  1015. }
  1016. #ifndef ACPI_USE_LOCAL_CACHE
  1017. /*******************************************************************************
  1018. *
  1019. * FUNCTION: acpi_os_create_cache
  1020. *
  1021. * PARAMETERS: name - Ascii name for the cache
  1022. * size - Size of each cached object
  1023. * depth - Maximum depth of the cache (in objects) <ignored>
  1024. * cache - Where the new cache object is returned
  1025. *
  1026. * RETURN: status
  1027. *
  1028. * DESCRIPTION: Create a cache object
  1029. *
  1030. ******************************************************************************/
  1031. acpi_status
  1032. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1033. {
  1034. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1035. if (*cache == NULL)
  1036. return AE_ERROR;
  1037. else
  1038. return AE_OK;
  1039. }
  1040. /*******************************************************************************
  1041. *
  1042. * FUNCTION: acpi_os_purge_cache
  1043. *
  1044. * PARAMETERS: Cache - Handle to cache object
  1045. *
  1046. * RETURN: Status
  1047. *
  1048. * DESCRIPTION: Free all objects within the requested cache.
  1049. *
  1050. ******************************************************************************/
  1051. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1052. {
  1053. kmem_cache_shrink(cache);
  1054. return (AE_OK);
  1055. }
  1056. /*******************************************************************************
  1057. *
  1058. * FUNCTION: acpi_os_delete_cache
  1059. *
  1060. * PARAMETERS: Cache - Handle to cache object
  1061. *
  1062. * RETURN: Status
  1063. *
  1064. * DESCRIPTION: Free all objects within the requested cache and delete the
  1065. * cache object.
  1066. *
  1067. ******************************************************************************/
  1068. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1069. {
  1070. kmem_cache_destroy(cache);
  1071. return (AE_OK);
  1072. }
  1073. /*******************************************************************************
  1074. *
  1075. * FUNCTION: acpi_os_release_object
  1076. *
  1077. * PARAMETERS: Cache - Handle to cache object
  1078. * Object - The object to be released
  1079. *
  1080. * RETURN: None
  1081. *
  1082. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1083. * the object is deleted.
  1084. *
  1085. ******************************************************************************/
  1086. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1087. {
  1088. kmem_cache_free(cache, object);
  1089. return (AE_OK);
  1090. }
  1091. /******************************************************************************
  1092. *
  1093. * FUNCTION: acpi_os_validate_interface
  1094. *
  1095. * PARAMETERS: interface - Requested interface to be validated
  1096. *
  1097. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  1098. *
  1099. * DESCRIPTION: Match an interface string to the interfaces supported by the
  1100. * host. Strings originate from an AML call to the _OSI method.
  1101. *
  1102. *****************************************************************************/
  1103. acpi_status
  1104. acpi_os_validate_interface (char *interface)
  1105. {
  1106. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  1107. return AE_OK;
  1108. if (!strcmp("Linux", interface)) {
  1109. printk(KERN_NOTICE PREFIX
  1110. "BIOS _OSI(Linux) query %s%s\n",
  1111. osi_linux.enable ? "honored" : "ignored",
  1112. osi_linux.cmdline ? " via cmdline" :
  1113. osi_linux.dmi ? " via DMI" : "");
  1114. if (osi_linux.enable)
  1115. return AE_OK;
  1116. }
  1117. return AE_SUPPORT;
  1118. }
  1119. static inline int acpi_res_list_add(struct acpi_res_list *res)
  1120. {
  1121. struct acpi_res_list *res_list_elem;
  1122. list_for_each_entry(res_list_elem, &resource_list_head,
  1123. resource_list) {
  1124. if (res->resource_type == res_list_elem->resource_type &&
  1125. res->start == res_list_elem->start &&
  1126. res->end == res_list_elem->end) {
  1127. /*
  1128. * The Region(addr,len) already exist in the list,
  1129. * just increase the count
  1130. */
  1131. res_list_elem->count++;
  1132. return 0;
  1133. }
  1134. }
  1135. res->count = 1;
  1136. list_add(&res->resource_list, &resource_list_head);
  1137. return 1;
  1138. }
  1139. static inline void acpi_res_list_del(struct acpi_res_list *res)
  1140. {
  1141. struct acpi_res_list *res_list_elem;
  1142. list_for_each_entry(res_list_elem, &resource_list_head,
  1143. resource_list) {
  1144. if (res->resource_type == res_list_elem->resource_type &&
  1145. res->start == res_list_elem->start &&
  1146. res->end == res_list_elem->end) {
  1147. /*
  1148. * If the res count is decreased to 0,
  1149. * remove and free it
  1150. */
  1151. if (--res_list_elem->count == 0) {
  1152. list_del(&res_list_elem->resource_list);
  1153. kfree(res_list_elem);
  1154. }
  1155. return;
  1156. }
  1157. }
  1158. }
  1159. acpi_status
  1160. acpi_os_invalidate_address(
  1161. u8 space_id,
  1162. acpi_physical_address address,
  1163. acpi_size length)
  1164. {
  1165. struct acpi_res_list res;
  1166. switch (space_id) {
  1167. case ACPI_ADR_SPACE_SYSTEM_IO:
  1168. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  1169. /* Only interference checks against SystemIO and SystemMemory
  1170. are needed */
  1171. res.start = address;
  1172. res.end = address + length - 1;
  1173. res.resource_type = space_id;
  1174. spin_lock(&acpi_res_lock);
  1175. acpi_res_list_del(&res);
  1176. spin_unlock(&acpi_res_lock);
  1177. break;
  1178. case ACPI_ADR_SPACE_PCI_CONFIG:
  1179. case ACPI_ADR_SPACE_EC:
  1180. case ACPI_ADR_SPACE_SMBUS:
  1181. case ACPI_ADR_SPACE_CMOS:
  1182. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  1183. case ACPI_ADR_SPACE_DATA_TABLE:
  1184. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  1185. break;
  1186. }
  1187. return AE_OK;
  1188. }
  1189. /******************************************************************************
  1190. *
  1191. * FUNCTION: acpi_os_validate_address
  1192. *
  1193. * PARAMETERS: space_id - ACPI space ID
  1194. * address - Physical address
  1195. * length - Address length
  1196. *
  1197. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  1198. * should return AE_AML_ILLEGAL_ADDRESS.
  1199. *
  1200. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  1201. * the addresses accessed by AML operation regions.
  1202. *
  1203. *****************************************************************************/
  1204. acpi_status
  1205. acpi_os_validate_address (
  1206. u8 space_id,
  1207. acpi_physical_address address,
  1208. acpi_size length,
  1209. char *name)
  1210. {
  1211. struct acpi_res_list *res;
  1212. int added;
  1213. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1214. return AE_OK;
  1215. switch (space_id) {
  1216. case ACPI_ADR_SPACE_SYSTEM_IO:
  1217. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  1218. /* Only interference checks against SystemIO and SystemMemory
  1219. are needed */
  1220. res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
  1221. if (!res)
  1222. return AE_OK;
  1223. /* ACPI names are fixed to 4 bytes, still better use strlcpy */
  1224. strlcpy(res->name, name, 5);
  1225. res->start = address;
  1226. res->end = address + length - 1;
  1227. res->resource_type = space_id;
  1228. spin_lock(&acpi_res_lock);
  1229. added = acpi_res_list_add(res);
  1230. spin_unlock(&acpi_res_lock);
  1231. pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
  1232. "name: %s\n", added ? "Added" : "Already exist",
  1233. (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  1234. ? "SystemIO" : "System Memory",
  1235. (unsigned long long)res->start,
  1236. (unsigned long long)res->end,
  1237. res->name);
  1238. if (!added)
  1239. kfree(res);
  1240. break;
  1241. case ACPI_ADR_SPACE_PCI_CONFIG:
  1242. case ACPI_ADR_SPACE_EC:
  1243. case ACPI_ADR_SPACE_SMBUS:
  1244. case ACPI_ADR_SPACE_CMOS:
  1245. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  1246. case ACPI_ADR_SPACE_DATA_TABLE:
  1247. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  1248. break;
  1249. }
  1250. return AE_OK;
  1251. }
  1252. #endif