osl.c 38 KB

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