acpiphp_glue.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. /*
  2. * ACPI PCI HotPlug glue functions to ACPI CA subsystem
  3. *
  4. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  5. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  6. * Copyright (C) 2002,2003 NEC Corporation
  7. * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
  8. * Copyright (C) 2003-2005 Hewlett Packard
  9. * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
  10. * Copyright (C) 2005 Intel Corporation
  11. *
  12. * All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  22. * NON INFRINGEMENT. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. * Send feedback to <kristen.c.accardi@intel.com>
  30. *
  31. */
  32. /*
  33. * Lifetime rules for pci_dev:
  34. * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
  35. * when the driver is loaded or when an insertion event occurs. It loses
  36. * a refcount when its ejected or the driver unloads.
  37. * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
  38. * when the bridge is scanned and it loses a refcount when the bridge
  39. * is removed.
  40. */
  41. #include <linux/init.h>
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/pci.h>
  45. #include <linux/pci_hotplug.h>
  46. #include <linux/mutex.h>
  47. #include "../pci.h"
  48. #include "acpiphp.h"
  49. static LIST_HEAD(bridge_list);
  50. static LIST_HEAD(ioapic_list);
  51. static DEFINE_SPINLOCK(ioapic_list_lock);
  52. #define MY_NAME "acpiphp_glue"
  53. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  54. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  55. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
  56. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  57. /*
  58. * initialization & terminatation routines
  59. */
  60. /**
  61. * is_ejectable - determine if a slot is ejectable
  62. * @handle: handle to acpi namespace
  63. *
  64. * Ejectable slot should satisfy at least these conditions:
  65. *
  66. * 1. has _ADR method
  67. * 2. has _EJ0 method
  68. *
  69. * optionally
  70. *
  71. * 1. has _STA method
  72. * 2. has _PS0 method
  73. * 3. has _PS3 method
  74. * 4. ..
  75. *
  76. */
  77. static int is_ejectable(acpi_handle handle)
  78. {
  79. acpi_status status;
  80. acpi_handle tmp;
  81. status = acpi_get_handle(handle, "_ADR", &tmp);
  82. if (ACPI_FAILURE(status)) {
  83. return 0;
  84. }
  85. status = acpi_get_handle(handle, "_EJ0", &tmp);
  86. if (ACPI_FAILURE(status)) {
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. /* callback routine to check the existence of ejectable slots */
  92. static acpi_status
  93. is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  94. {
  95. int *count = (int *)context;
  96. if (is_ejectable(handle)) {
  97. (*count)++;
  98. /* only one ejectable slot is enough */
  99. return AE_CTRL_TERMINATE;
  100. } else {
  101. return AE_OK;
  102. }
  103. }
  104. /* callback routine to check for the existance of a pci dock device */
  105. static acpi_status
  106. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  107. {
  108. int *count = (int *)context;
  109. if (is_dock_device(handle)) {
  110. (*count)++;
  111. return AE_CTRL_TERMINATE;
  112. } else {
  113. return AE_OK;
  114. }
  115. }
  116. /*
  117. * the _DCK method can do funny things... and sometimes not
  118. * hah-hah funny.
  119. *
  120. * TBD - figure out a way to only call fixups for
  121. * systems that require them.
  122. */
  123. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  124. void *v)
  125. {
  126. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  127. struct pci_bus *bus = func->slot->bridge->pci_bus;
  128. u32 buses;
  129. if (!bus->self)
  130. return NOTIFY_OK;
  131. /* fixup bad _DCK function that rewrites
  132. * secondary bridge on slot
  133. */
  134. pci_read_config_dword(bus->self,
  135. PCI_PRIMARY_BUS,
  136. &buses);
  137. if (((buses >> 8) & 0xff) != bus->secondary) {
  138. buses = (buses & 0xff000000)
  139. | ((unsigned int)(bus->primary) << 0)
  140. | ((unsigned int)(bus->secondary) << 8)
  141. | ((unsigned int)(bus->subordinate) << 16);
  142. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  143. }
  144. return NOTIFY_OK;
  145. }
  146. /* callback routine to register each ACPI PCI slot object */
  147. static acpi_status
  148. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  149. {
  150. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  151. struct acpiphp_slot *slot;
  152. struct acpiphp_func *newfunc;
  153. acpi_handle tmp;
  154. acpi_status status = AE_OK;
  155. unsigned long adr, sun;
  156. int device, function, retval;
  157. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  158. if (ACPI_FAILURE(status))
  159. return AE_OK;
  160. status = acpi_get_handle(handle, "_EJ0", &tmp);
  161. if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
  162. return AE_OK;
  163. device = (adr >> 16) & 0xffff;
  164. function = adr & 0xffff;
  165. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  166. if (!newfunc)
  167. return AE_NO_MEMORY;
  168. INIT_LIST_HEAD(&newfunc->sibling);
  169. newfunc->handle = handle;
  170. newfunc->function = function;
  171. if (ACPI_SUCCESS(status))
  172. newfunc->flags = FUNC_HAS_EJ0;
  173. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  174. newfunc->flags |= FUNC_HAS_STA;
  175. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  176. newfunc->flags |= FUNC_HAS_PS0;
  177. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  178. newfunc->flags |= FUNC_HAS_PS3;
  179. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  180. newfunc->flags |= FUNC_HAS_DCK;
  181. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  182. if (ACPI_FAILURE(status)) {
  183. /*
  184. * use the count of the number of slots we've found
  185. * for the number of the slot
  186. */
  187. sun = bridge->nr_slots+1;
  188. }
  189. /* search for objects that share the same slot */
  190. for (slot = bridge->slots; slot; slot = slot->next)
  191. if (slot->device == device) {
  192. if (slot->sun != sun)
  193. warn("sibling found, but _SUN doesn't match!\n");
  194. break;
  195. }
  196. if (!slot) {
  197. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  198. if (!slot) {
  199. kfree(newfunc);
  200. return AE_NO_MEMORY;
  201. }
  202. slot->bridge = bridge;
  203. slot->device = device;
  204. slot->sun = sun;
  205. INIT_LIST_HEAD(&slot->funcs);
  206. mutex_init(&slot->crit_sect);
  207. slot->next = bridge->slots;
  208. bridge->slots = slot;
  209. bridge->nr_slots++;
  210. dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
  211. slot->sun, pci_domain_nr(bridge->pci_bus),
  212. bridge->pci_bus->number, slot->device);
  213. retval = acpiphp_register_hotplug_slot(slot);
  214. if (retval) {
  215. warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
  216. goto err_exit;
  217. }
  218. }
  219. newfunc->slot = slot;
  220. list_add_tail(&newfunc->sibling, &slot->funcs);
  221. /* associate corresponding pci_dev */
  222. newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
  223. PCI_DEVFN(device, function));
  224. if (newfunc->pci_dev) {
  225. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  226. }
  227. if (is_dock_device(handle)) {
  228. /* we don't want to call this device's _EJ0
  229. * because we want the dock notify handler
  230. * to call it after it calls _DCK
  231. */
  232. newfunc->flags &= ~FUNC_HAS_EJ0;
  233. if (register_hotplug_dock_device(handle,
  234. handle_hotplug_event_func, newfunc))
  235. dbg("failed to register dock device\n");
  236. /* we need to be notified when dock events happen
  237. * outside of the hotplug operation, since we may
  238. * need to do fixups before we can hotplug.
  239. */
  240. newfunc->nb.notifier_call = post_dock_fixups;
  241. if (register_dock_notifier(&newfunc->nb))
  242. dbg("failed to register a dock notifier");
  243. }
  244. /* install notify handler */
  245. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  246. status = acpi_install_notify_handler(handle,
  247. ACPI_SYSTEM_NOTIFY,
  248. handle_hotplug_event_func,
  249. newfunc);
  250. if (ACPI_FAILURE(status))
  251. err("failed to register interrupt notify handler\n");
  252. } else
  253. status = AE_OK;
  254. return status;
  255. err_exit:
  256. bridge->nr_slots--;
  257. bridge->slots = slot->next;
  258. kfree(slot);
  259. kfree(newfunc);
  260. return AE_OK;
  261. }
  262. /* see if it's worth looking at this bridge */
  263. static int detect_ejectable_slots(acpi_handle *bridge_handle)
  264. {
  265. acpi_status status;
  266. int count;
  267. count = 0;
  268. /* only check slots defined directly below bridge object */
  269. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
  270. is_ejectable_slot, (void *)&count, NULL);
  271. /*
  272. * we also need to add this bridge if there is a dock bridge or
  273. * other pci device on a dock station (removable)
  274. */
  275. if (!count)
  276. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
  277. (u32)1, is_pci_dock_device, (void *)&count,
  278. NULL);
  279. return count;
  280. }
  281. /* decode ACPI 2.0 _HPP hot plug parameters */
  282. static void decode_hpp(struct acpiphp_bridge *bridge)
  283. {
  284. acpi_status status;
  285. status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
  286. if (ACPI_FAILURE(status) ||
  287. !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
  288. /* use default numbers */
  289. printk(KERN_WARNING
  290. "%s: Could not get hotplug parameters. Use defaults\n",
  291. __FUNCTION__);
  292. bridge->hpp.t0 = &bridge->hpp.type0_data;
  293. bridge->hpp.t0->revision = 0;
  294. bridge->hpp.t0->cache_line_size = 0x10;
  295. bridge->hpp.t0->latency_timer = 0x40;
  296. bridge->hpp.t0->enable_serr = 0;
  297. bridge->hpp.t0->enable_perr = 0;
  298. }
  299. }
  300. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  301. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  302. {
  303. acpi_status status;
  304. /* decode ACPI 2.0 _HPP (hot plug parameters) */
  305. decode_hpp(bridge);
  306. /* must be added to the list prior to calling register_slot */
  307. list_add(&bridge->list, &bridge_list);
  308. /* register all slot objects under this bridge */
  309. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  310. register_slot, bridge, NULL);
  311. if (ACPI_FAILURE(status)) {
  312. list_del(&bridge->list);
  313. return;
  314. }
  315. /* install notify handler */
  316. if (bridge->type != BRIDGE_TYPE_HOST) {
  317. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  318. status = acpi_remove_notify_handler(bridge->func->handle,
  319. ACPI_SYSTEM_NOTIFY,
  320. handle_hotplug_event_func);
  321. if (ACPI_FAILURE(status))
  322. err("failed to remove notify handler\n");
  323. }
  324. status = acpi_install_notify_handler(bridge->handle,
  325. ACPI_SYSTEM_NOTIFY,
  326. handle_hotplug_event_bridge,
  327. bridge);
  328. if (ACPI_FAILURE(status)) {
  329. err("failed to register interrupt notify handler\n");
  330. }
  331. }
  332. }
  333. /* find acpiphp_func from acpiphp_bridge */
  334. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  335. {
  336. struct list_head *node, *l;
  337. struct acpiphp_bridge *bridge;
  338. struct acpiphp_slot *slot;
  339. struct acpiphp_func *func;
  340. list_for_each(node, &bridge_list) {
  341. bridge = list_entry(node, struct acpiphp_bridge, list);
  342. for (slot = bridge->slots; slot; slot = slot->next) {
  343. list_for_each(l, &slot->funcs) {
  344. func = list_entry(l, struct acpiphp_func,
  345. sibling);
  346. if (func->handle == handle)
  347. return func;
  348. }
  349. }
  350. }
  351. return NULL;
  352. }
  353. static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
  354. {
  355. acpi_handle dummy_handle;
  356. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  357. "_STA", &dummy_handle)))
  358. bridge->flags |= BRIDGE_HAS_STA;
  359. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  360. "_EJ0", &dummy_handle)))
  361. bridge->flags |= BRIDGE_HAS_EJ0;
  362. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  363. "_PS0", &dummy_handle)))
  364. bridge->flags |= BRIDGE_HAS_PS0;
  365. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  366. "_PS3", &dummy_handle)))
  367. bridge->flags |= BRIDGE_HAS_PS3;
  368. /* is this ejectable p2p bridge? */
  369. if (bridge->flags & BRIDGE_HAS_EJ0) {
  370. struct acpiphp_func *func;
  371. dbg("found ejectable p2p bridge\n");
  372. /* make link between PCI bridge and PCI function */
  373. func = acpiphp_bridge_handle_to_function(bridge->handle);
  374. if (!func)
  375. return;
  376. bridge->func = func;
  377. func->bridge = bridge;
  378. }
  379. }
  380. /* allocate and initialize host bridge data structure */
  381. static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
  382. {
  383. struct acpiphp_bridge *bridge;
  384. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  385. if (bridge == NULL)
  386. return;
  387. bridge->type = BRIDGE_TYPE_HOST;
  388. bridge->handle = handle;
  389. bridge->pci_bus = pci_bus;
  390. spin_lock_init(&bridge->res_lock);
  391. init_bridge_misc(bridge);
  392. }
  393. /* allocate and initialize PCI-to-PCI bridge data structure */
  394. static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
  395. {
  396. struct acpiphp_bridge *bridge;
  397. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  398. if (bridge == NULL) {
  399. err("out of memory\n");
  400. return;
  401. }
  402. bridge->type = BRIDGE_TYPE_P2P;
  403. bridge->handle = handle;
  404. config_p2p_bridge_flags(bridge);
  405. bridge->pci_dev = pci_dev_get(pci_dev);
  406. bridge->pci_bus = pci_dev->subordinate;
  407. if (!bridge->pci_bus) {
  408. err("This is not a PCI-to-PCI bridge!\n");
  409. goto err;
  410. }
  411. spin_lock_init(&bridge->res_lock);
  412. init_bridge_misc(bridge);
  413. return;
  414. err:
  415. pci_dev_put(pci_dev);
  416. kfree(bridge);
  417. return;
  418. }
  419. /* callback routine to find P2P bridges */
  420. static acpi_status
  421. find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  422. {
  423. acpi_status status;
  424. acpi_handle dummy_handle;
  425. unsigned long tmp;
  426. int device, function;
  427. struct pci_dev *dev;
  428. struct pci_bus *pci_bus = context;
  429. status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  430. if (ACPI_FAILURE(status))
  431. return AE_OK; /* continue */
  432. status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
  433. if (ACPI_FAILURE(status)) {
  434. dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
  435. return AE_OK;
  436. }
  437. device = (tmp >> 16) & 0xffff;
  438. function = tmp & 0xffff;
  439. dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
  440. if (!dev || !dev->subordinate)
  441. goto out;
  442. /* check if this bridge has ejectable slots */
  443. if ((detect_ejectable_slots(handle) > 0)) {
  444. dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
  445. add_p2p_bridge(handle, dev);
  446. }
  447. /* search P2P bridges under this p2p bridge */
  448. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  449. find_p2p_bridge, dev->subordinate, NULL);
  450. if (ACPI_FAILURE(status))
  451. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  452. out:
  453. pci_dev_put(dev);
  454. return AE_OK;
  455. }
  456. /* find hot-pluggable slots, and then find P2P bridge */
  457. static int add_bridge(acpi_handle handle)
  458. {
  459. acpi_status status;
  460. unsigned long tmp;
  461. int seg, bus;
  462. acpi_handle dummy_handle;
  463. struct pci_bus *pci_bus;
  464. /* if the bridge doesn't have _STA, we assume it is always there */
  465. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  466. if (ACPI_SUCCESS(status)) {
  467. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  468. if (ACPI_FAILURE(status)) {
  469. dbg("%s: _STA evaluation failure\n", __FUNCTION__);
  470. return 0;
  471. }
  472. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  473. /* don't register this object */
  474. return 0;
  475. }
  476. /* get PCI segment number */
  477. status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
  478. seg = ACPI_SUCCESS(status) ? tmp : 0;
  479. /* get PCI bus number */
  480. status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
  481. if (ACPI_SUCCESS(status)) {
  482. bus = tmp;
  483. } else {
  484. warn("can't get bus number, assuming 0\n");
  485. bus = 0;
  486. }
  487. pci_bus = pci_find_bus(seg, bus);
  488. if (!pci_bus) {
  489. err("Can't find bus %04x:%02x\n", seg, bus);
  490. return 0;
  491. }
  492. /* check if this bridge has ejectable slots */
  493. if (detect_ejectable_slots(handle) > 0) {
  494. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  495. add_host_bridge(handle, pci_bus);
  496. }
  497. /* search P2P bridges under this host bridge */
  498. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  499. find_p2p_bridge, pci_bus, NULL);
  500. if (ACPI_FAILURE(status))
  501. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  502. return 0;
  503. }
  504. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  505. {
  506. struct list_head *head;
  507. list_for_each(head, &bridge_list) {
  508. struct acpiphp_bridge *bridge = list_entry(head,
  509. struct acpiphp_bridge, list);
  510. if (bridge->handle == handle)
  511. return bridge;
  512. }
  513. return NULL;
  514. }
  515. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  516. {
  517. struct list_head *list, *tmp;
  518. struct acpiphp_slot *slot;
  519. acpi_status status;
  520. acpi_handle handle = bridge->handle;
  521. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  522. handle_hotplug_event_bridge);
  523. if (ACPI_FAILURE(status))
  524. err("failed to remove notify handler\n");
  525. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  526. ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
  527. status = acpi_install_notify_handler(bridge->func->handle,
  528. ACPI_SYSTEM_NOTIFY,
  529. handle_hotplug_event_func,
  530. bridge->func);
  531. if (ACPI_FAILURE(status))
  532. err("failed to install interrupt notify handler\n");
  533. }
  534. slot = bridge->slots;
  535. while (slot) {
  536. struct acpiphp_slot *next = slot->next;
  537. list_for_each_safe (list, tmp, &slot->funcs) {
  538. struct acpiphp_func *func;
  539. func = list_entry(list, struct acpiphp_func, sibling);
  540. if (is_dock_device(func->handle)) {
  541. unregister_hotplug_dock_device(func->handle);
  542. unregister_dock_notifier(&func->nb);
  543. }
  544. if (!(func->flags & FUNC_HAS_DCK)) {
  545. status = acpi_remove_notify_handler(func->handle,
  546. ACPI_SYSTEM_NOTIFY,
  547. handle_hotplug_event_func);
  548. if (ACPI_FAILURE(status))
  549. err("failed to remove notify handler\n");
  550. }
  551. pci_dev_put(func->pci_dev);
  552. list_del(list);
  553. kfree(func);
  554. }
  555. acpiphp_unregister_hotplug_slot(slot);
  556. list_del(&slot->funcs);
  557. kfree(slot);
  558. slot = next;
  559. }
  560. pci_dev_put(bridge->pci_dev);
  561. list_del(&bridge->list);
  562. kfree(bridge);
  563. }
  564. static acpi_status
  565. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  566. {
  567. struct acpiphp_bridge *bridge;
  568. /* cleanup p2p bridges under this P2P bridge
  569. in a depth-first manner */
  570. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  571. cleanup_p2p_bridge, NULL, NULL);
  572. if (!(bridge = acpiphp_handle_to_bridge(handle)))
  573. return AE_OK;
  574. cleanup_bridge(bridge);
  575. return AE_OK;
  576. }
  577. static void remove_bridge(acpi_handle handle)
  578. {
  579. struct acpiphp_bridge *bridge;
  580. /* cleanup p2p bridges under this host bridge
  581. in a depth-first manner */
  582. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  583. (u32)1, cleanup_p2p_bridge, NULL, NULL);
  584. bridge = acpiphp_handle_to_bridge(handle);
  585. if (bridge)
  586. cleanup_bridge(bridge);
  587. }
  588. static struct pci_dev * get_apic_pci_info(acpi_handle handle)
  589. {
  590. struct acpi_pci_id id;
  591. struct pci_bus *bus;
  592. struct pci_dev *dev;
  593. if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
  594. return NULL;
  595. bus = pci_find_bus(id.segment, id.bus);
  596. if (!bus)
  597. return NULL;
  598. dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
  599. if (!dev)
  600. return NULL;
  601. if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
  602. (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
  603. {
  604. pci_dev_put(dev);
  605. return NULL;
  606. }
  607. return dev;
  608. }
  609. static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
  610. {
  611. acpi_status status;
  612. int result = -1;
  613. unsigned long gsb;
  614. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  615. union acpi_object *obj;
  616. void *table;
  617. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
  618. if (ACPI_SUCCESS(status)) {
  619. *gsi_base = (u32)gsb;
  620. return 0;
  621. }
  622. status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
  623. if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
  624. return -1;
  625. obj = buffer.pointer;
  626. if (obj->type != ACPI_TYPE_BUFFER)
  627. goto out;
  628. table = obj->buffer.pointer;
  629. switch (((struct acpi_subtable_header *)table)->type) {
  630. case ACPI_MADT_TYPE_IO_SAPIC:
  631. *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
  632. result = 0;
  633. break;
  634. case ACPI_MADT_TYPE_IO_APIC:
  635. *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
  636. result = 0;
  637. break;
  638. default:
  639. break;
  640. }
  641. out:
  642. kfree(buffer.pointer);
  643. return result;
  644. }
  645. static acpi_status
  646. ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
  647. {
  648. acpi_status status;
  649. unsigned long sta;
  650. acpi_handle tmp;
  651. struct pci_dev *pdev;
  652. u32 gsi_base;
  653. u64 phys_addr;
  654. struct acpiphp_ioapic *ioapic;
  655. /* Evaluate _STA if present */
  656. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  657. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  658. return AE_CTRL_DEPTH;
  659. /* Scan only PCI bus scope */
  660. status = acpi_get_handle(handle, "_HID", &tmp);
  661. if (ACPI_SUCCESS(status))
  662. return AE_CTRL_DEPTH;
  663. if (get_gsi_base(handle, &gsi_base))
  664. return AE_OK;
  665. ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
  666. if (!ioapic)
  667. return AE_NO_MEMORY;
  668. pdev = get_apic_pci_info(handle);
  669. if (!pdev)
  670. goto exit_kfree;
  671. if (pci_enable_device(pdev))
  672. goto exit_pci_dev_put;
  673. pci_set_master(pdev);
  674. if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
  675. goto exit_pci_disable_device;
  676. phys_addr = pci_resource_start(pdev, 0);
  677. if (acpi_register_ioapic(handle, phys_addr, gsi_base))
  678. goto exit_pci_release_region;
  679. ioapic->gsi_base = gsi_base;
  680. ioapic->dev = pdev;
  681. spin_lock(&ioapic_list_lock);
  682. list_add_tail(&ioapic->list, &ioapic_list);
  683. spin_unlock(&ioapic_list_lock);
  684. return AE_OK;
  685. exit_pci_release_region:
  686. pci_release_region(pdev, 0);
  687. exit_pci_disable_device:
  688. pci_disable_device(pdev);
  689. exit_pci_dev_put:
  690. pci_dev_put(pdev);
  691. exit_kfree:
  692. kfree(ioapic);
  693. return AE_OK;
  694. }
  695. static acpi_status
  696. ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
  697. {
  698. acpi_status status;
  699. unsigned long sta;
  700. acpi_handle tmp;
  701. u32 gsi_base;
  702. struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
  703. /* Evaluate _STA if present */
  704. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  705. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  706. return AE_CTRL_DEPTH;
  707. /* Scan only PCI bus scope */
  708. status = acpi_get_handle(handle, "_HID", &tmp);
  709. if (ACPI_SUCCESS(status))
  710. return AE_CTRL_DEPTH;
  711. if (get_gsi_base(handle, &gsi_base))
  712. return AE_OK;
  713. acpi_unregister_ioapic(handle, gsi_base);
  714. spin_lock(&ioapic_list_lock);
  715. list_for_each_entry_safe(pos, n, &ioapic_list, list) {
  716. if (pos->gsi_base != gsi_base)
  717. continue;
  718. ioapic = pos;
  719. list_del(&ioapic->list);
  720. break;
  721. }
  722. spin_unlock(&ioapic_list_lock);
  723. if (!ioapic)
  724. return AE_OK;
  725. pci_release_region(ioapic->dev, 0);
  726. pci_disable_device(ioapic->dev);
  727. pci_dev_put(ioapic->dev);
  728. kfree(ioapic);
  729. return AE_OK;
  730. }
  731. static int acpiphp_configure_ioapics(acpi_handle handle)
  732. {
  733. ioapic_add(handle, 0, NULL, NULL);
  734. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  735. ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
  736. return 0;
  737. }
  738. static int acpiphp_unconfigure_ioapics(acpi_handle handle)
  739. {
  740. ioapic_remove(handle, 0, NULL, NULL);
  741. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  742. ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
  743. return 0;
  744. }
  745. static int power_on_slot(struct acpiphp_slot *slot)
  746. {
  747. acpi_status status;
  748. struct acpiphp_func *func;
  749. struct list_head *l;
  750. int retval = 0;
  751. /* if already enabled, just skip */
  752. if (slot->flags & SLOT_POWEREDON)
  753. goto err_exit;
  754. list_for_each (l, &slot->funcs) {
  755. func = list_entry(l, struct acpiphp_func, sibling);
  756. if (func->flags & FUNC_HAS_PS0) {
  757. dbg("%s: executing _PS0\n", __FUNCTION__);
  758. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  759. if (ACPI_FAILURE(status)) {
  760. warn("%s: _PS0 failed\n", __FUNCTION__);
  761. retval = -1;
  762. goto err_exit;
  763. } else
  764. break;
  765. }
  766. }
  767. /* TBD: evaluate _STA to check if the slot is enabled */
  768. slot->flags |= SLOT_POWEREDON;
  769. err_exit:
  770. return retval;
  771. }
  772. static int power_off_slot(struct acpiphp_slot *slot)
  773. {
  774. acpi_status status;
  775. struct acpiphp_func *func;
  776. struct list_head *l;
  777. int retval = 0;
  778. /* if already disabled, just skip */
  779. if ((slot->flags & SLOT_POWEREDON) == 0)
  780. goto err_exit;
  781. list_for_each (l, &slot->funcs) {
  782. func = list_entry(l, struct acpiphp_func, sibling);
  783. if (func->flags & FUNC_HAS_PS3) {
  784. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  785. if (ACPI_FAILURE(status)) {
  786. warn("%s: _PS3 failed\n", __FUNCTION__);
  787. retval = -1;
  788. goto err_exit;
  789. } else
  790. break;
  791. }
  792. }
  793. /* TBD: evaluate _STA to check if the slot is disabled */
  794. slot->flags &= (~SLOT_POWEREDON);
  795. err_exit:
  796. return retval;
  797. }
  798. /**
  799. * acpiphp_max_busnr - return the highest reserved bus number under
  800. * the given bus.
  801. * @bus: bus to start search with
  802. *
  803. */
  804. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  805. {
  806. struct list_head *tmp;
  807. unsigned char max, n;
  808. /*
  809. * pci_bus_max_busnr will return the highest
  810. * reserved busnr for all these children.
  811. * that is equivalent to the bus->subordinate
  812. * value. We don't want to use the parent's
  813. * bus->subordinate value because it could have
  814. * padding in it.
  815. */
  816. max = bus->secondary;
  817. list_for_each(tmp, &bus->children) {
  818. n = pci_bus_max_busnr(pci_bus_b(tmp));
  819. if (n > max)
  820. max = n;
  821. }
  822. return max;
  823. }
  824. /**
  825. * acpiphp_bus_add - add a new bus to acpi subsystem
  826. * @func: acpiphp_func of the bridge
  827. *
  828. */
  829. static int acpiphp_bus_add(struct acpiphp_func *func)
  830. {
  831. acpi_handle phandle;
  832. struct acpi_device *device, *pdevice;
  833. int ret_val;
  834. acpi_get_parent(func->handle, &phandle);
  835. if (acpi_bus_get_device(phandle, &pdevice)) {
  836. dbg("no parent device, assuming NULL\n");
  837. pdevice = NULL;
  838. }
  839. if (!acpi_bus_get_device(func->handle, &device)) {
  840. dbg("bus exists... trim\n");
  841. /* this shouldn't be in here, so remove
  842. * the bus then re-add it...
  843. */
  844. ret_val = acpi_bus_trim(device, 1);
  845. dbg("acpi_bus_trim return %x\n", ret_val);
  846. }
  847. ret_val = acpi_bus_add(&device, pdevice, func->handle,
  848. ACPI_BUS_TYPE_DEVICE);
  849. if (ret_val) {
  850. dbg("error adding bus, %x\n",
  851. -ret_val);
  852. goto acpiphp_bus_add_out;
  853. }
  854. /*
  855. * try to start anyway. We could have failed to add
  856. * simply because this bus had previously been added
  857. * on another add. Don't bother with the return value
  858. * we just keep going.
  859. */
  860. ret_val = acpi_bus_start(device);
  861. acpiphp_bus_add_out:
  862. return ret_val;
  863. }
  864. /**
  865. * acpiphp_bus_trim - trim a bus from acpi subsystem
  866. * @handle: handle to acpi namespace
  867. *
  868. */
  869. static int acpiphp_bus_trim(acpi_handle handle)
  870. {
  871. struct acpi_device *device;
  872. int retval;
  873. retval = acpi_bus_get_device(handle, &device);
  874. if (retval) {
  875. dbg("acpi_device not found\n");
  876. return retval;
  877. }
  878. retval = acpi_bus_trim(device, 1);
  879. if (retval)
  880. err("cannot remove from acpi list\n");
  881. return retval;
  882. }
  883. /**
  884. * enable_device - enable, configure a slot
  885. * @slot: slot to be enabled
  886. *
  887. * This function should be called per *physical slot*,
  888. * not per each slot object in ACPI namespace.
  889. *
  890. */
  891. static int enable_device(struct acpiphp_slot *slot)
  892. {
  893. struct pci_dev *dev;
  894. struct pci_bus *bus = slot->bridge->pci_bus;
  895. struct list_head *l;
  896. struct acpiphp_func *func;
  897. int retval = 0;
  898. int num, max, pass;
  899. acpi_status status;
  900. if (slot->flags & SLOT_ENABLED)
  901. goto err_exit;
  902. /* sanity check: dev should be NULL when hot-plugged in */
  903. dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  904. if (dev) {
  905. /* This case shouldn't happen */
  906. err("pci_dev structure already exists.\n");
  907. pci_dev_put(dev);
  908. retval = -1;
  909. goto err_exit;
  910. }
  911. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  912. if (num == 0) {
  913. err("No new device found\n");
  914. retval = -1;
  915. goto err_exit;
  916. }
  917. max = acpiphp_max_busnr(bus);
  918. for (pass = 0; pass < 2; pass++) {
  919. list_for_each_entry(dev, &bus->devices, bus_list) {
  920. if (PCI_SLOT(dev->devfn) != slot->device)
  921. continue;
  922. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  923. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  924. max = pci_scan_bridge(bus, dev, max, pass);
  925. if (pass && dev->subordinate)
  926. pci_bus_size_bridges(dev->subordinate);
  927. }
  928. }
  929. }
  930. list_for_each (l, &slot->funcs) {
  931. func = list_entry(l, struct acpiphp_func, sibling);
  932. acpiphp_bus_add(func);
  933. }
  934. pci_bus_assign_resources(bus);
  935. acpiphp_sanitize_bus(bus);
  936. acpiphp_set_hpp_values(slot->bridge->handle, bus);
  937. list_for_each_entry(func, &slot->funcs, sibling)
  938. acpiphp_configure_ioapics(func->handle);
  939. pci_enable_bridges(bus);
  940. pci_bus_add_devices(bus);
  941. /* associate pci_dev to our representation */
  942. list_for_each (l, &slot->funcs) {
  943. func = list_entry(l, struct acpiphp_func, sibling);
  944. func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  945. func->function));
  946. if (!func->pci_dev)
  947. continue;
  948. if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  949. func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
  950. continue;
  951. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  952. if (ACPI_FAILURE(status))
  953. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  954. status);
  955. }
  956. slot->flags |= SLOT_ENABLED;
  957. err_exit:
  958. return retval;
  959. }
  960. static void disable_bridges(struct pci_bus *bus)
  961. {
  962. struct pci_dev *dev;
  963. list_for_each_entry(dev, &bus->devices, bus_list) {
  964. if (dev->subordinate) {
  965. disable_bridges(dev->subordinate);
  966. pci_disable_device(dev);
  967. }
  968. }
  969. }
  970. /**
  971. * disable_device - disable a slot
  972. */
  973. static int disable_device(struct acpiphp_slot *slot)
  974. {
  975. int retval = 0;
  976. struct acpiphp_func *func;
  977. struct list_head *l;
  978. /* is this slot already disabled? */
  979. if (!(slot->flags & SLOT_ENABLED))
  980. goto err_exit;
  981. list_for_each (l, &slot->funcs) {
  982. func = list_entry(l, struct acpiphp_func, sibling);
  983. if (func->bridge) {
  984. /* cleanup p2p bridges under this P2P bridge */
  985. cleanup_p2p_bridge(func->bridge->handle,
  986. (u32)1, NULL, NULL);
  987. func->bridge = NULL;
  988. }
  989. if (func->pci_dev) {
  990. pci_stop_bus_device(func->pci_dev);
  991. if (func->pci_dev->subordinate) {
  992. disable_bridges(func->pci_dev->subordinate);
  993. pci_disable_device(func->pci_dev);
  994. }
  995. }
  996. }
  997. list_for_each (l, &slot->funcs) {
  998. func = list_entry(l, struct acpiphp_func, sibling);
  999. acpiphp_unconfigure_ioapics(func->handle);
  1000. acpiphp_bus_trim(func->handle);
  1001. /* try to remove anyway.
  1002. * acpiphp_bus_add might have been failed */
  1003. if (!func->pci_dev)
  1004. continue;
  1005. pci_remove_bus_device(func->pci_dev);
  1006. pci_dev_put(func->pci_dev);
  1007. func->pci_dev = NULL;
  1008. }
  1009. slot->flags &= (~SLOT_ENABLED);
  1010. err_exit:
  1011. return retval;
  1012. }
  1013. /**
  1014. * get_slot_status - get ACPI slot status
  1015. *
  1016. * if a slot has _STA for each function and if any one of them
  1017. * returned non-zero status, return it
  1018. *
  1019. * if a slot doesn't have _STA and if any one of its functions'
  1020. * configuration space is configured, return 0x0f as a _STA
  1021. *
  1022. * otherwise return 0
  1023. */
  1024. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  1025. {
  1026. acpi_status status;
  1027. unsigned long sta = 0;
  1028. u32 dvid;
  1029. struct list_head *l;
  1030. struct acpiphp_func *func;
  1031. list_for_each (l, &slot->funcs) {
  1032. func = list_entry(l, struct acpiphp_func, sibling);
  1033. if (func->flags & FUNC_HAS_STA) {
  1034. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  1035. if (ACPI_SUCCESS(status) && sta)
  1036. break;
  1037. } else {
  1038. pci_bus_read_config_dword(slot->bridge->pci_bus,
  1039. PCI_DEVFN(slot->device,
  1040. func->function),
  1041. PCI_VENDOR_ID, &dvid);
  1042. if (dvid != 0xffffffff) {
  1043. sta = ACPI_STA_ALL;
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. return (unsigned int)sta;
  1049. }
  1050. /**
  1051. * acpiphp_eject_slot - physically eject the slot
  1052. */
  1053. static int acpiphp_eject_slot(struct acpiphp_slot *slot)
  1054. {
  1055. acpi_status status;
  1056. struct acpiphp_func *func;
  1057. struct list_head *l;
  1058. struct acpi_object_list arg_list;
  1059. union acpi_object arg;
  1060. list_for_each (l, &slot->funcs) {
  1061. func = list_entry(l, struct acpiphp_func, sibling);
  1062. /* We don't want to call _EJ0 on non-existing functions. */
  1063. if ((func->flags & FUNC_HAS_EJ0)) {
  1064. /* _EJ0 method take one argument */
  1065. arg_list.count = 1;
  1066. arg_list.pointer = &arg;
  1067. arg.type = ACPI_TYPE_INTEGER;
  1068. arg.integer.value = 1;
  1069. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  1070. if (ACPI_FAILURE(status)) {
  1071. warn("%s: _EJ0 failed\n", __FUNCTION__);
  1072. return -1;
  1073. } else
  1074. break;
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. /**
  1080. * acpiphp_check_bridge - re-enumerate devices
  1081. *
  1082. * Iterate over all slots under this bridge and make sure that if a
  1083. * card is present they are enabled, and if not they are disabled.
  1084. */
  1085. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  1086. {
  1087. struct acpiphp_slot *slot;
  1088. int retval = 0;
  1089. int enabled, disabled;
  1090. enabled = disabled = 0;
  1091. for (slot = bridge->slots; slot; slot = slot->next) {
  1092. unsigned int status = get_slot_status(slot);
  1093. if (slot->flags & SLOT_ENABLED) {
  1094. if (status == ACPI_STA_ALL)
  1095. continue;
  1096. retval = acpiphp_disable_slot(slot);
  1097. if (retval) {
  1098. err("Error occurred in disabling\n");
  1099. goto err_exit;
  1100. } else {
  1101. acpiphp_eject_slot(slot);
  1102. }
  1103. disabled++;
  1104. } else {
  1105. if (status != ACPI_STA_ALL)
  1106. continue;
  1107. retval = acpiphp_enable_slot(slot);
  1108. if (retval) {
  1109. err("Error occurred in enabling\n");
  1110. goto err_exit;
  1111. }
  1112. enabled++;
  1113. }
  1114. }
  1115. dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
  1116. err_exit:
  1117. return retval;
  1118. }
  1119. static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
  1120. {
  1121. u16 pci_cmd, pci_bctl;
  1122. struct pci_dev *cdev;
  1123. /* Program hpp values for this device */
  1124. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  1125. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  1126. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  1127. return;
  1128. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  1129. bridge->hpp.t0->cache_line_size);
  1130. pci_write_config_byte(dev, PCI_LATENCY_TIMER,
  1131. bridge->hpp.t0->latency_timer);
  1132. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1133. if (bridge->hpp.t0->enable_serr)
  1134. pci_cmd |= PCI_COMMAND_SERR;
  1135. else
  1136. pci_cmd &= ~PCI_COMMAND_SERR;
  1137. if (bridge->hpp.t0->enable_perr)
  1138. pci_cmd |= PCI_COMMAND_PARITY;
  1139. else
  1140. pci_cmd &= ~PCI_COMMAND_PARITY;
  1141. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1142. /* Program bridge control value and child devices */
  1143. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1144. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1145. bridge->hpp.t0->latency_timer);
  1146. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1147. if (bridge->hpp.t0->enable_serr)
  1148. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1149. else
  1150. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  1151. if (bridge->hpp.t0->enable_perr)
  1152. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1153. else
  1154. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  1155. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1156. if (dev->subordinate) {
  1157. list_for_each_entry(cdev, &dev->subordinate->devices,
  1158. bus_list)
  1159. program_hpp(cdev, bridge);
  1160. }
  1161. }
  1162. }
  1163. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
  1164. {
  1165. struct acpiphp_bridge bridge;
  1166. struct pci_dev *dev;
  1167. memset(&bridge, 0, sizeof(bridge));
  1168. bridge.handle = handle;
  1169. bridge.pci_bus = bus;
  1170. bridge.pci_dev = bus->self;
  1171. decode_hpp(&bridge);
  1172. list_for_each_entry(dev, &bus->devices, bus_list)
  1173. program_hpp(dev, &bridge);
  1174. }
  1175. /*
  1176. * Remove devices for which we could not assign resources, call
  1177. * arch specific code to fix-up the bus
  1178. */
  1179. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  1180. {
  1181. struct pci_dev *dev;
  1182. int i;
  1183. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  1184. list_for_each_entry(dev, &bus->devices, bus_list) {
  1185. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  1186. struct resource *res = &dev->resource[i];
  1187. if ((res->flags & type_mask) && !res->start &&
  1188. res->end) {
  1189. /* Could not assign a required resources
  1190. * for this device, remove it */
  1191. pci_remove_bus_device(dev);
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. /* Program resources in newly inserted bridge */
  1198. static int acpiphp_configure_bridge (acpi_handle handle)
  1199. {
  1200. struct acpi_pci_id pci_id;
  1201. struct pci_bus *bus;
  1202. if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
  1203. err("cannot get PCI domain and bus number for bridge\n");
  1204. return -EINVAL;
  1205. }
  1206. bus = pci_find_bus(pci_id.segment, pci_id.bus);
  1207. if (!bus) {
  1208. err("cannot find bus %d:%d\n",
  1209. pci_id.segment, pci_id.bus);
  1210. return -EINVAL;
  1211. }
  1212. pci_bus_size_bridges(bus);
  1213. pci_bus_assign_resources(bus);
  1214. acpiphp_sanitize_bus(bus);
  1215. acpiphp_set_hpp_values(handle, bus);
  1216. pci_enable_bridges(bus);
  1217. acpiphp_configure_ioapics(handle);
  1218. return 0;
  1219. }
  1220. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  1221. {
  1222. struct acpi_device *device, *pdevice;
  1223. acpi_handle phandle;
  1224. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  1225. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  1226. err("unexpected notification type %d\n", type);
  1227. return;
  1228. }
  1229. acpi_get_parent(handle, &phandle);
  1230. if (acpi_bus_get_device(phandle, &pdevice)) {
  1231. dbg("no parent device, assuming NULL\n");
  1232. pdevice = NULL;
  1233. }
  1234. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  1235. err("cannot add bridge to acpi list\n");
  1236. return;
  1237. }
  1238. if (!acpiphp_configure_bridge(handle) &&
  1239. !acpi_bus_start(device))
  1240. add_bridge(handle);
  1241. else
  1242. err("cannot configure and start bridge\n");
  1243. }
  1244. /*
  1245. * ACPI event handlers
  1246. */
  1247. /**
  1248. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1249. *
  1250. * @handle: Notify()'ed acpi_handle
  1251. * @type: Notify code
  1252. * @context: pointer to acpiphp_bridge structure
  1253. *
  1254. * handles ACPI event notification on {host,p2p} bridges
  1255. *
  1256. */
  1257. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  1258. {
  1259. struct acpiphp_bridge *bridge;
  1260. char objname[64];
  1261. struct acpi_buffer buffer = { .length = sizeof(objname),
  1262. .pointer = objname };
  1263. struct acpi_device *device;
  1264. if (acpi_bus_get_device(handle, &device)) {
  1265. /* This bridge must have just been physically inserted */
  1266. handle_bridge_insertion(handle, type);
  1267. return;
  1268. }
  1269. bridge = acpiphp_handle_to_bridge(handle);
  1270. if (!bridge) {
  1271. err("cannot get bridge info\n");
  1272. return;
  1273. }
  1274. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1275. switch (type) {
  1276. case ACPI_NOTIFY_BUS_CHECK:
  1277. /* bus re-enumerate */
  1278. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  1279. acpiphp_check_bridge(bridge);
  1280. break;
  1281. case ACPI_NOTIFY_DEVICE_CHECK:
  1282. /* device check */
  1283. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  1284. acpiphp_check_bridge(bridge);
  1285. break;
  1286. case ACPI_NOTIFY_DEVICE_WAKE:
  1287. /* wake event */
  1288. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  1289. break;
  1290. case ACPI_NOTIFY_EJECT_REQUEST:
  1291. /* request device eject */
  1292. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  1293. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1294. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1295. struct acpiphp_slot *slot;
  1296. slot = bridge->func->slot;
  1297. if (!acpiphp_disable_slot(slot))
  1298. acpiphp_eject_slot(slot);
  1299. }
  1300. break;
  1301. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1302. printk(KERN_ERR "Device %s cannot be configured due"
  1303. " to a frequency mismatch\n", objname);
  1304. break;
  1305. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1306. printk(KERN_ERR "Device %s cannot be configured due"
  1307. " to a bus mode mismatch\n", objname);
  1308. break;
  1309. case ACPI_NOTIFY_POWER_FAULT:
  1310. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1311. objname);
  1312. break;
  1313. default:
  1314. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1315. break;
  1316. }
  1317. }
  1318. /**
  1319. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1320. *
  1321. * @handle: Notify()'ed acpi_handle
  1322. * @type: Notify code
  1323. * @context: pointer to acpiphp_func structure
  1324. *
  1325. * handles ACPI event notification on slots
  1326. *
  1327. */
  1328. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  1329. {
  1330. struct acpiphp_func *func;
  1331. char objname[64];
  1332. struct acpi_buffer buffer = { .length = sizeof(objname),
  1333. .pointer = objname };
  1334. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1335. func = (struct acpiphp_func *)context;
  1336. switch (type) {
  1337. case ACPI_NOTIFY_BUS_CHECK:
  1338. /* bus re-enumerate */
  1339. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  1340. acpiphp_enable_slot(func->slot);
  1341. break;
  1342. case ACPI_NOTIFY_DEVICE_CHECK:
  1343. /* device check : re-enumerate from parent bus */
  1344. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  1345. acpiphp_check_bridge(func->slot->bridge);
  1346. break;
  1347. case ACPI_NOTIFY_DEVICE_WAKE:
  1348. /* wake event */
  1349. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  1350. break;
  1351. case ACPI_NOTIFY_EJECT_REQUEST:
  1352. /* request device eject */
  1353. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  1354. if (!(acpiphp_disable_slot(func->slot)))
  1355. acpiphp_eject_slot(func->slot);
  1356. break;
  1357. default:
  1358. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1359. break;
  1360. }
  1361. }
  1362. static acpi_status
  1363. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1364. {
  1365. int *count = (int *)context;
  1366. if (acpi_root_bridge(handle)) {
  1367. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1368. handle_hotplug_event_bridge, NULL);
  1369. (*count)++;
  1370. }
  1371. return AE_OK ;
  1372. }
  1373. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1374. .add = add_bridge,
  1375. .remove = remove_bridge,
  1376. };
  1377. /**
  1378. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1379. *
  1380. */
  1381. int __init acpiphp_glue_init(void)
  1382. {
  1383. int num = 0;
  1384. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1385. ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
  1386. if (num <= 0)
  1387. return -1;
  1388. else
  1389. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1390. return 0;
  1391. }
  1392. /**
  1393. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1394. *
  1395. * This function frees all data allocated in acpiphp_glue_init()
  1396. */
  1397. void acpiphp_glue_exit(void)
  1398. {
  1399. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1400. }
  1401. /**
  1402. * acpiphp_get_num_slots - count number of slots in a system
  1403. */
  1404. int __init acpiphp_get_num_slots(void)
  1405. {
  1406. struct acpiphp_bridge *bridge;
  1407. int num_slots = 0;
  1408. list_for_each_entry (bridge, &bridge_list, list) {
  1409. dbg("Bus %04x:%02x has %d slot%s\n",
  1410. pci_domain_nr(bridge->pci_bus),
  1411. bridge->pci_bus->number, bridge->nr_slots,
  1412. bridge->nr_slots == 1 ? "" : "s");
  1413. num_slots += bridge->nr_slots;
  1414. }
  1415. dbg("Total %d slots\n", num_slots);
  1416. return num_slots;
  1417. }
  1418. #if 0
  1419. /**
  1420. * acpiphp_for_each_slot - call function for each slot
  1421. * @fn: callback function
  1422. * @data: context to be passed to callback function
  1423. *
  1424. */
  1425. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1426. {
  1427. struct list_head *node;
  1428. struct acpiphp_bridge *bridge;
  1429. struct acpiphp_slot *slot;
  1430. int retval = 0;
  1431. list_for_each (node, &bridge_list) {
  1432. bridge = (struct acpiphp_bridge *)node;
  1433. for (slot = bridge->slots; slot; slot = slot->next) {
  1434. retval = fn(slot, data);
  1435. if (!retval)
  1436. goto err_exit;
  1437. }
  1438. }
  1439. err_exit:
  1440. return retval;
  1441. }
  1442. #endif
  1443. /**
  1444. * acpiphp_enable_slot - power on slot
  1445. */
  1446. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1447. {
  1448. int retval;
  1449. mutex_lock(&slot->crit_sect);
  1450. /* wake up all functions */
  1451. retval = power_on_slot(slot);
  1452. if (retval)
  1453. goto err_exit;
  1454. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1455. /* configure all functions */
  1456. retval = enable_device(slot);
  1457. if (retval)
  1458. power_off_slot(slot);
  1459. } else {
  1460. dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
  1461. power_off_slot(slot);
  1462. }
  1463. err_exit:
  1464. mutex_unlock(&slot->crit_sect);
  1465. return retval;
  1466. }
  1467. /**
  1468. * acpiphp_disable_slot - power off slot
  1469. */
  1470. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1471. {
  1472. int retval = 0;
  1473. mutex_lock(&slot->crit_sect);
  1474. /* unconfigure all functions */
  1475. retval = disable_device(slot);
  1476. if (retval)
  1477. goto err_exit;
  1478. /* power off all functions */
  1479. retval = power_off_slot(slot);
  1480. if (retval)
  1481. goto err_exit;
  1482. err_exit:
  1483. mutex_unlock(&slot->crit_sect);
  1484. return retval;
  1485. }
  1486. /*
  1487. * slot enabled: 1
  1488. * slot disabled: 0
  1489. */
  1490. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1491. {
  1492. return (slot->flags & SLOT_POWEREDON);
  1493. }
  1494. /*
  1495. * latch open: 1
  1496. * latch closed: 0
  1497. */
  1498. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1499. {
  1500. unsigned int sta;
  1501. sta = get_slot_status(slot);
  1502. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1503. }
  1504. /*
  1505. * adapter presence : 1
  1506. * absence : 0
  1507. */
  1508. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1509. {
  1510. unsigned int sta;
  1511. sta = get_slot_status(slot);
  1512. return (sta == 0) ? 0 : 1;
  1513. }
  1514. /*
  1515. * pci address (seg/bus/dev)
  1516. */
  1517. u32 acpiphp_get_address(struct acpiphp_slot *slot)
  1518. {
  1519. u32 address;
  1520. struct pci_bus *pci_bus = slot->bridge->pci_bus;
  1521. address = (pci_domain_nr(pci_bus) << 16) |
  1522. (pci_bus->number << 8) |
  1523. slot->device;
  1524. return address;
  1525. }