osl.c 37 KB

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