pciehprm_acpi.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /*
  2. * PCIEHPRM ACPI: PHP Resource Manager for ACPI platform
  3. *
  4. * Copyright (C) 2003-2004 Intel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send feedback to <kristen.c.accardi@intel.com>
  24. *
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/pci.h>
  31. #include <linux/init.h>
  32. #include <linux/acpi.h>
  33. #include <linux/efi.h>
  34. #include <linux/pci-acpi.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/system.h>
  37. #ifdef CONFIG_IA64
  38. #include <asm/iosapic.h>
  39. #endif
  40. #include <acpi/acpi.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/actypes.h>
  43. #include "pciehp.h"
  44. #include "pciehprm.h"
  45. #define PCI_MAX_BUS 0x100
  46. #define ACPI_STA_DEVICE_PRESENT 0x01
  47. #define METHOD_NAME__SUN "_SUN"
  48. #define METHOD_NAME__HPP "_HPP"
  49. #define METHOD_NAME_OSHP "OSHP"
  50. /* Status code for running acpi method to gain native control */
  51. #define NC_NOT_RUN 0
  52. #define OSC_NOT_EXIST 1
  53. #define OSC_RUN_FAILED 2
  54. #define OSHP_NOT_EXIST 3
  55. #define OSHP_RUN_FAILED 4
  56. #define NC_RUN_SUCCESS 5
  57. #define PHP_RES_BUS 0xA0
  58. #define PHP_RES_IO 0xA1
  59. #define PHP_RES_MEM 0xA2
  60. #define PHP_RES_PMEM 0xA3
  61. #define BRIDGE_TYPE_P2P 0x00
  62. #define BRIDGE_TYPE_HOST 0x01
  63. /* this should go to drivers/acpi/include/ */
  64. struct acpi__hpp {
  65. u8 cache_line_size;
  66. u8 latency_timer;
  67. u8 enable_serr;
  68. u8 enable_perr;
  69. };
  70. struct acpi_php_slot {
  71. struct acpi_php_slot *next;
  72. struct acpi_bridge *bridge;
  73. acpi_handle handle;
  74. int seg;
  75. int bus;
  76. int dev;
  77. int fun;
  78. u32 sun;
  79. struct pci_resource *mem_head;
  80. struct pci_resource *p_mem_head;
  81. struct pci_resource *io_head;
  82. struct pci_resource *bus_head;
  83. void *slot_ops; /* _STA, _EJx, etc */
  84. struct slot *slot;
  85. }; /* per func */
  86. struct acpi_bridge {
  87. struct acpi_bridge *parent;
  88. struct acpi_bridge *next;
  89. struct acpi_bridge *child;
  90. acpi_handle handle;
  91. int seg;
  92. int pbus; /* pdev->bus->number */
  93. int pdevice; /* PCI_SLOT(pdev->devfn) */
  94. int pfunction; /* PCI_DEVFN(pdev->devfn) */
  95. int bus; /* pdev->subordinate->number */
  96. struct acpi__hpp *_hpp;
  97. struct acpi_php_slot *slots;
  98. struct pci_resource *tmem_head; /* total from crs */
  99. struct pci_resource *tp_mem_head; /* total from crs */
  100. struct pci_resource *tio_head; /* total from crs */
  101. struct pci_resource *tbus_head; /* total from crs */
  102. struct pci_resource *mem_head; /* available */
  103. struct pci_resource *p_mem_head; /* available */
  104. struct pci_resource *io_head; /* available */
  105. struct pci_resource *bus_head; /* available */
  106. int scanned;
  107. int type;
  108. };
  109. static struct acpi_bridge *acpi_bridges_head;
  110. static u8 * acpi_path_name( acpi_handle handle)
  111. {
  112. acpi_status status;
  113. static u8 path_name[ACPI_PATHNAME_MAX];
  114. struct acpi_buffer ret_buf = { ACPI_PATHNAME_MAX, path_name };
  115. memset(path_name, 0, sizeof (path_name));
  116. status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &ret_buf);
  117. if (ACPI_FAILURE(status))
  118. return NULL;
  119. else
  120. return path_name;
  121. }
  122. static void acpi_get__hpp ( struct acpi_bridge *ab);
  123. static int acpi_run_oshp ( struct acpi_bridge *ab);
  124. static int osc_run_status = NC_NOT_RUN;
  125. static int oshp_run_status = NC_NOT_RUN;
  126. static int acpi_add_slot_to_php_slots(
  127. struct acpi_bridge *ab,
  128. int bus_num,
  129. acpi_handle handle,
  130. u32 adr,
  131. u32 sun
  132. )
  133. {
  134. struct acpi_php_slot *aps;
  135. static long samesun = -1;
  136. aps = (struct acpi_php_slot *) kmalloc (sizeof(struct acpi_php_slot), GFP_KERNEL);
  137. if (!aps) {
  138. err ("acpi_pciehprm: alloc for aps fail\n");
  139. return -1;
  140. }
  141. memset(aps, 0, sizeof(struct acpi_php_slot));
  142. aps->handle = handle;
  143. aps->bus = bus_num;
  144. aps->dev = (adr >> 16) & 0xffff;
  145. aps->fun = adr & 0xffff;
  146. aps->sun = sun;
  147. aps->next = ab->slots; /* cling to the bridge */
  148. aps->bridge = ab;
  149. ab->slots = aps;
  150. ab->scanned += 1;
  151. if (!ab->_hpp)
  152. acpi_get__hpp(ab);
  153. if (osc_run_status == OSC_NOT_EXIST)
  154. oshp_run_status = acpi_run_oshp(ab);
  155. if (sun != samesun) {
  156. info("acpi_pciehprm: Slot sun(%x) at s:b:d:f=0x%02x:%02x:%02x:%02x\n",
  157. aps->sun, ab->seg, aps->bus, aps->dev, aps->fun);
  158. samesun = sun;
  159. }
  160. return 0;
  161. }
  162. static void acpi_get__hpp ( struct acpi_bridge *ab)
  163. {
  164. acpi_status status;
  165. u8 nui[4];
  166. struct acpi_buffer ret_buf = { 0, NULL};
  167. union acpi_object *ext_obj, *package;
  168. u8 *path_name = acpi_path_name(ab->handle);
  169. int i, len = 0;
  170. /* get _hpp */
  171. status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
  172. switch (status) {
  173. case AE_BUFFER_OVERFLOW:
  174. ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
  175. if (!ret_buf.pointer) {
  176. err ("acpi_pciehprm:%s alloc for _HPP fail\n", path_name);
  177. return;
  178. }
  179. status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
  180. if (ACPI_SUCCESS(status))
  181. break;
  182. default:
  183. if (ACPI_FAILURE(status)) {
  184. err("acpi_pciehprm:%s _HPP fail=0x%x\n", path_name, status);
  185. return;
  186. }
  187. }
  188. ext_obj = (union acpi_object *) ret_buf.pointer;
  189. if (ext_obj->type != ACPI_TYPE_PACKAGE) {
  190. err ("acpi_pciehprm:%s _HPP obj not a package\n", path_name);
  191. goto free_and_return;
  192. }
  193. len = ext_obj->package.count;
  194. package = (union acpi_object *) ret_buf.pointer;
  195. for ( i = 0; (i < len) || (i < 4); i++) {
  196. ext_obj = (union acpi_object *) &package->package.elements[i];
  197. switch (ext_obj->type) {
  198. case ACPI_TYPE_INTEGER:
  199. nui[i] = (u8)ext_obj->integer.value;
  200. break;
  201. default:
  202. err ("acpi_pciehprm:%s _HPP obj type incorrect\n", path_name);
  203. goto free_and_return;
  204. }
  205. }
  206. ab->_hpp = kmalloc (sizeof (struct acpi__hpp), GFP_KERNEL);
  207. if (!ab->_hpp) {
  208. err ("acpi_pciehprm:%s alloc for _HPP failed\n", path_name);
  209. goto free_and_return;
  210. }
  211. memset(ab->_hpp, 0, sizeof(struct acpi__hpp));
  212. ab->_hpp->cache_line_size = nui[0];
  213. ab->_hpp->latency_timer = nui[1];
  214. ab->_hpp->enable_serr = nui[2];
  215. ab->_hpp->enable_perr = nui[3];
  216. dbg(" _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
  217. dbg(" _HPP: latency timer =0x%x\n", ab->_hpp->latency_timer);
  218. dbg(" _HPP: enable SERR =0x%x\n", ab->_hpp->enable_serr);
  219. dbg(" _HPP: enable PERR =0x%x\n", ab->_hpp->enable_perr);
  220. free_and_return:
  221. kfree(ret_buf.pointer);
  222. }
  223. static int acpi_run_oshp ( struct acpi_bridge *ab)
  224. {
  225. acpi_status status;
  226. u8 *path_name = acpi_path_name(ab->handle);
  227. /* run OSHP */
  228. status = acpi_evaluate_object(ab->handle, METHOD_NAME_OSHP, NULL, NULL);
  229. if (ACPI_FAILURE(status)) {
  230. err("acpi_pciehprm:%s OSHP fails=0x%x\n", path_name, status);
  231. oshp_run_status = (status == AE_NOT_FOUND) ? OSHP_NOT_EXIST : OSHP_RUN_FAILED;
  232. } else {
  233. oshp_run_status = NC_RUN_SUCCESS;
  234. dbg("acpi_pciehprm:%s OSHP passes =0x%x\n", path_name, status);
  235. dbg("acpi_pciehprm:%s oshp_run_status =0x%x\n", path_name, oshp_run_status);
  236. }
  237. return oshp_run_status;
  238. }
  239. static acpi_status acpi_evaluate_crs(
  240. acpi_handle handle,
  241. struct acpi_resource **retbuf
  242. )
  243. {
  244. acpi_status status;
  245. struct acpi_buffer crsbuf;
  246. u8 *path_name = acpi_path_name(handle);
  247. crsbuf.length = 0;
  248. crsbuf.pointer = NULL;
  249. status = acpi_get_current_resources (handle, &crsbuf);
  250. switch (status) {
  251. case AE_BUFFER_OVERFLOW:
  252. break; /* found */
  253. case AE_NOT_FOUND:
  254. dbg("acpi_pciehprm:%s _CRS not found\n", path_name);
  255. return status;
  256. default:
  257. err ("acpi_pciehprm:%s _CRS fail=0x%x\n", path_name, status);
  258. return status;
  259. }
  260. crsbuf.pointer = kmalloc (crsbuf.length, GFP_KERNEL);
  261. if (!crsbuf.pointer) {
  262. err ("acpi_pciehprm: alloc %ld bytes for %s _CRS fail\n", (ulong)crsbuf.length, path_name);
  263. return AE_NO_MEMORY;
  264. }
  265. status = acpi_get_current_resources (handle, &crsbuf);
  266. if (ACPI_FAILURE(status)) {
  267. err("acpi_pciehprm: %s _CRS fail=0x%x.\n", path_name, status);
  268. kfree(crsbuf.pointer);
  269. return status;
  270. }
  271. *retbuf = crsbuf.pointer;
  272. return status;
  273. }
  274. static void free_pci_resource ( struct pci_resource *aprh)
  275. {
  276. struct pci_resource *res, *next;
  277. for (res = aprh; res; res = next) {
  278. next = res->next;
  279. kfree(res);
  280. }
  281. }
  282. static void print_pci_resource ( struct pci_resource *aprh)
  283. {
  284. struct pci_resource *res;
  285. for (res = aprh; res; res = res->next)
  286. dbg(" base= 0x%x length= 0x%x\n", res->base, res->length);
  287. }
  288. static void print_slot_resources( struct acpi_php_slot *aps)
  289. {
  290. if (aps->bus_head) {
  291. dbg(" BUS Resources:\n");
  292. print_pci_resource (aps->bus_head);
  293. }
  294. if (aps->io_head) {
  295. dbg(" IO Resources:\n");
  296. print_pci_resource (aps->io_head);
  297. }
  298. if (aps->mem_head) {
  299. dbg(" MEM Resources:\n");
  300. print_pci_resource (aps->mem_head);
  301. }
  302. if (aps->p_mem_head) {
  303. dbg(" PMEM Resources:\n");
  304. print_pci_resource (aps->p_mem_head);
  305. }
  306. }
  307. static void print_pci_resources( struct acpi_bridge *ab)
  308. {
  309. if (ab->tbus_head) {
  310. dbg(" Total BUS Resources:\n");
  311. print_pci_resource (ab->tbus_head);
  312. }
  313. if (ab->bus_head) {
  314. dbg(" BUS Resources:\n");
  315. print_pci_resource (ab->bus_head);
  316. }
  317. if (ab->tio_head) {
  318. dbg(" Total IO Resources:\n");
  319. print_pci_resource (ab->tio_head);
  320. }
  321. if (ab->io_head) {
  322. dbg(" IO Resources:\n");
  323. print_pci_resource (ab->io_head);
  324. }
  325. if (ab->tmem_head) {
  326. dbg(" Total MEM Resources:\n");
  327. print_pci_resource (ab->tmem_head);
  328. }
  329. if (ab->mem_head) {
  330. dbg(" MEM Resources:\n");
  331. print_pci_resource (ab->mem_head);
  332. }
  333. if (ab->tp_mem_head) {
  334. dbg(" Total PMEM Resources:\n");
  335. print_pci_resource (ab->tp_mem_head);
  336. }
  337. if (ab->p_mem_head) {
  338. dbg(" PMEM Resources:\n");
  339. print_pci_resource (ab->p_mem_head);
  340. }
  341. if (ab->_hpp) {
  342. dbg(" _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
  343. dbg(" _HPP: latency timer =0x%x\n", ab->_hpp->latency_timer);
  344. dbg(" _HPP: enable SERR =0x%x\n", ab->_hpp->enable_serr);
  345. dbg(" _HPP: enable PERR =0x%x\n", ab->_hpp->enable_perr);
  346. }
  347. }
  348. static int pciehprm_delete_resource(
  349. struct pci_resource **aprh,
  350. ulong base,
  351. ulong size)
  352. {
  353. struct pci_resource *res;
  354. struct pci_resource *prevnode;
  355. struct pci_resource *split_node;
  356. ulong tbase;
  357. pciehp_resource_sort_and_combine(aprh);
  358. for (res = *aprh; res; res = res->next) {
  359. if (res->base > base)
  360. continue;
  361. if ((res->base + res->length) < (base + size))
  362. continue;
  363. if (res->base < base) {
  364. tbase = base;
  365. if ((res->length - (tbase - res->base)) < size)
  366. continue;
  367. split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  368. if (!split_node)
  369. return -ENOMEM;
  370. split_node->base = res->base;
  371. split_node->length = tbase - res->base;
  372. res->base = tbase;
  373. res->length -= split_node->length;
  374. split_node->next = res->next;
  375. res->next = split_node;
  376. }
  377. if (res->length >= size) {
  378. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  379. if (!split_node)
  380. return -ENOMEM;
  381. split_node->base = res->base + size;
  382. split_node->length = res->length - size;
  383. res->length = size;
  384. split_node->next = res->next;
  385. res->next = split_node;
  386. }
  387. if (*aprh == res) {
  388. *aprh = res->next;
  389. } else {
  390. prevnode = *aprh;
  391. while (prevnode->next != res)
  392. prevnode = prevnode->next;
  393. prevnode->next = res->next;
  394. }
  395. res->next = NULL;
  396. kfree(res);
  397. break;
  398. }
  399. return 0;
  400. }
  401. static int pciehprm_delete_resources(
  402. struct pci_resource **aprh,
  403. struct pci_resource *this
  404. )
  405. {
  406. struct pci_resource *res;
  407. for (res = this; res; res = res->next)
  408. pciehprm_delete_resource(aprh, res->base, res->length);
  409. return 0;
  410. }
  411. static int pciehprm_add_resource(
  412. struct pci_resource **aprh,
  413. ulong base,
  414. ulong size)
  415. {
  416. struct pci_resource *res;
  417. for (res = *aprh; res; res = res->next) {
  418. if ((res->base + res->length) == base) {
  419. res->length += size;
  420. size = 0L;
  421. break;
  422. }
  423. if (res->next == *aprh)
  424. break;
  425. }
  426. if (size) {
  427. res = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  428. if (!res) {
  429. err ("acpi_pciehprm: alloc for res fail\n");
  430. return -ENOMEM;
  431. }
  432. memset(res, 0, sizeof (struct pci_resource));
  433. res->base = base;
  434. res->length = size;
  435. res->next = *aprh;
  436. *aprh = res;
  437. }
  438. return 0;
  439. }
  440. static int pciehprm_add_resources(
  441. struct pci_resource **aprh,
  442. struct pci_resource *this
  443. )
  444. {
  445. struct pci_resource *res;
  446. int rc = 0;
  447. for (res = this; res && !rc; res = res->next)
  448. rc = pciehprm_add_resource(aprh, res->base, res->length);
  449. return rc;
  450. }
  451. static void acpi_parse_io (
  452. struct acpi_bridge *ab,
  453. union acpi_resource_data *data
  454. )
  455. {
  456. struct acpi_resource_io *dataio;
  457. dataio = (struct acpi_resource_io *) data;
  458. dbg("Io Resource\n");
  459. dbg(" %d bit decode\n", ACPI_DECODE_16 == dataio->io_decode ? 16:10);
  460. dbg(" Range minimum base: %08X\n", dataio->min_base_address);
  461. dbg(" Range maximum base: %08X\n", dataio->max_base_address);
  462. dbg(" Alignment: %08X\n", dataio->alignment);
  463. dbg(" Range Length: %08X\n", dataio->range_length);
  464. }
  465. static void acpi_parse_fixed_io (
  466. struct acpi_bridge *ab,
  467. union acpi_resource_data *data
  468. )
  469. {
  470. struct acpi_resource_fixed_io *datafio;
  471. datafio = (struct acpi_resource_fixed_io *) data;
  472. dbg("Fixed Io Resource\n");
  473. dbg(" Range base address: %08X", datafio->base_address);
  474. dbg(" Range length: %08X", datafio->range_length);
  475. }
  476. static void acpi_parse_address16_32 (
  477. struct acpi_bridge *ab,
  478. union acpi_resource_data *data,
  479. acpi_resource_type id
  480. )
  481. {
  482. /*
  483. * acpi_resource_address16 == acpi_resource_address32
  484. * acpi_resource_address16 *data16 = (acpi_resource_address16 *) data;
  485. */
  486. struct acpi_resource_address32 *data32 = (struct acpi_resource_address32 *) data;
  487. struct pci_resource **aprh, **tprh;
  488. if (id == ACPI_RSTYPE_ADDRESS16)
  489. dbg("acpi_pciehprm:16-Bit Address Space Resource\n");
  490. else
  491. dbg("acpi_pciehprm:32-Bit Address Space Resource\n");
  492. switch (data32->resource_type) {
  493. case ACPI_MEMORY_RANGE:
  494. dbg(" Resource Type: Memory Range\n");
  495. aprh = &ab->mem_head;
  496. tprh = &ab->tmem_head;
  497. switch (data32->attribute.memory.cache_attribute) {
  498. case ACPI_NON_CACHEABLE_MEMORY:
  499. dbg(" Type Specific: Noncacheable memory\n");
  500. break;
  501. case ACPI_CACHABLE_MEMORY:
  502. dbg(" Type Specific: Cacheable memory\n");
  503. break;
  504. case ACPI_WRITE_COMBINING_MEMORY:
  505. dbg(" Type Specific: Write-combining memory\n");
  506. break;
  507. case ACPI_PREFETCHABLE_MEMORY:
  508. aprh = &ab->p_mem_head;
  509. dbg(" Type Specific: Prefetchable memory\n");
  510. break;
  511. default:
  512. dbg(" Type Specific: Invalid cache attribute\n");
  513. break;
  514. }
  515. dbg(" Type Specific: Read%s\n", ACPI_READ_WRITE_MEMORY == data32->attribute.memory.read_write_attribute ? "/Write":" Only");
  516. break;
  517. case ACPI_IO_RANGE:
  518. dbg(" Resource Type: I/O Range\n");
  519. aprh = &ab->io_head;
  520. tprh = &ab->tio_head;
  521. switch (data32->attribute.io.range_attribute) {
  522. case ACPI_NON_ISA_ONLY_RANGES:
  523. dbg(" Type Specific: Non-ISA Io Addresses\n");
  524. break;
  525. case ACPI_ISA_ONLY_RANGES:
  526. dbg(" Type Specific: ISA Io Addresses\n");
  527. break;
  528. case ACPI_ENTIRE_RANGE:
  529. dbg(" Type Specific: ISA and non-ISA Io Addresses\n");
  530. break;
  531. default:
  532. dbg(" Type Specific: Invalid range attribute\n");
  533. break;
  534. }
  535. break;
  536. case ACPI_BUS_NUMBER_RANGE:
  537. dbg(" Resource Type: Bus Number Range(fixed)\n");
  538. /* fixup to be compatible with the rest of php driver */
  539. data32->min_address_range++;
  540. data32->address_length--;
  541. aprh = &ab->bus_head;
  542. tprh = &ab->tbus_head;
  543. break;
  544. default:
  545. dbg(" Resource Type: Invalid resource type. Exiting.\n");
  546. return;
  547. }
  548. dbg(" Resource %s\n", ACPI_CONSUMER == data32->producer_consumer ? "Consumer":"Producer");
  549. dbg(" %s decode\n", ACPI_SUB_DECODE == data32->decode ? "Subtractive":"Positive");
  550. dbg(" Min address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->min_address_fixed ? "":"not");
  551. dbg(" Max address is %s fixed\n", ACPI_ADDRESS_FIXED == data32->max_address_fixed ? "":"not");
  552. dbg(" Granularity: %08X\n", data32->granularity);
  553. dbg(" Address range min: %08X\n", data32->min_address_range);
  554. dbg(" Address range max: %08X\n", data32->max_address_range);
  555. dbg(" Address translation offset: %08X\n", data32->address_translation_offset);
  556. dbg(" Address Length: %08X\n", data32->address_length);
  557. if (0xFF != data32->resource_source.index) {
  558. dbg(" Resource Source Index: %X\n", data32->resource_source.index);
  559. /* dbg(" Resource Source: %s\n", data32->resource_source.string_ptr); */
  560. }
  561. pciehprm_add_resource(aprh, data32->min_address_range, data32->address_length);
  562. }
  563. static acpi_status acpi_parse_crs(
  564. struct acpi_bridge *ab,
  565. struct acpi_resource *crsbuf
  566. )
  567. {
  568. acpi_status status = AE_OK;
  569. struct acpi_resource *resource = crsbuf;
  570. u8 count = 0;
  571. u8 done = 0;
  572. while (!done) {
  573. dbg("acpi_pciehprm: PCI bus 0x%x Resource structure %x.\n", ab->bus, count++);
  574. switch (resource->id) {
  575. case ACPI_RSTYPE_IRQ:
  576. dbg("Irq -------- Resource\n");
  577. break;
  578. case ACPI_RSTYPE_DMA:
  579. dbg("DMA -------- Resource\n");
  580. break;
  581. case ACPI_RSTYPE_START_DPF:
  582. dbg("Start DPF -------- Resource\n");
  583. break;
  584. case ACPI_RSTYPE_END_DPF:
  585. dbg("End DPF -------- Resource\n");
  586. break;
  587. case ACPI_RSTYPE_IO:
  588. acpi_parse_io (ab, &resource->data);
  589. break;
  590. case ACPI_RSTYPE_FIXED_IO:
  591. acpi_parse_fixed_io (ab, &resource->data);
  592. break;
  593. case ACPI_RSTYPE_VENDOR:
  594. dbg("Vendor -------- Resource\n");
  595. break;
  596. case ACPI_RSTYPE_END_TAG:
  597. dbg("End_tag -------- Resource\n");
  598. done = 1;
  599. break;
  600. case ACPI_RSTYPE_MEM24:
  601. dbg("Mem24 -------- Resource\n");
  602. break;
  603. case ACPI_RSTYPE_MEM32:
  604. dbg("Mem32 -------- Resource\n");
  605. break;
  606. case ACPI_RSTYPE_FIXED_MEM32:
  607. dbg("Fixed Mem32 -------- Resource\n");
  608. break;
  609. case ACPI_RSTYPE_ADDRESS16:
  610. acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS16);
  611. break;
  612. case ACPI_RSTYPE_ADDRESS32:
  613. acpi_parse_address16_32(ab, &resource->data, ACPI_RSTYPE_ADDRESS32);
  614. break;
  615. case ACPI_RSTYPE_ADDRESS64:
  616. info("Address64 -------- Resource unparsed\n");
  617. break;
  618. case ACPI_RSTYPE_EXT_IRQ:
  619. dbg("Ext Irq -------- Resource\n");
  620. break;
  621. default:
  622. dbg("Invalid -------- resource type 0x%x\n", resource->id);
  623. break;
  624. }
  625. resource = (struct acpi_resource *) ((char *)resource + resource->length);
  626. }
  627. return status;
  628. }
  629. static acpi_status acpi_get_crs( struct acpi_bridge *ab)
  630. {
  631. acpi_status status;
  632. struct acpi_resource *crsbuf;
  633. status = acpi_evaluate_crs(ab->handle, &crsbuf);
  634. if (ACPI_SUCCESS(status)) {
  635. status = acpi_parse_crs(ab, crsbuf);
  636. kfree(crsbuf);
  637. pciehp_resource_sort_and_combine(&ab->bus_head);
  638. pciehp_resource_sort_and_combine(&ab->io_head);
  639. pciehp_resource_sort_and_combine(&ab->mem_head);
  640. pciehp_resource_sort_and_combine(&ab->p_mem_head);
  641. pciehprm_add_resources (&ab->tbus_head, ab->bus_head);
  642. pciehprm_add_resources (&ab->tio_head, ab->io_head);
  643. pciehprm_add_resources (&ab->tmem_head, ab->mem_head);
  644. pciehprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);
  645. }
  646. return status;
  647. }
  648. /* find acpi_bridge downword from ab. */
  649. static struct acpi_bridge *
  650. find_acpi_bridge_by_bus(
  651. struct acpi_bridge *ab,
  652. int seg,
  653. int bus /* pdev->subordinate->number */
  654. )
  655. {
  656. struct acpi_bridge *lab = NULL;
  657. if (!ab)
  658. return NULL;
  659. if ((ab->bus == bus) && (ab->seg == seg))
  660. return ab;
  661. if (ab->child)
  662. lab = find_acpi_bridge_by_bus(ab->child, seg, bus);
  663. if (!lab)
  664. if (ab->next)
  665. lab = find_acpi_bridge_by_bus(ab->next, seg, bus);
  666. return lab;
  667. }
  668. /*
  669. * Build a device tree of ACPI PCI Bridges
  670. */
  671. static void pciehprm_acpi_register_a_bridge (
  672. struct acpi_bridge **head,
  673. struct acpi_bridge *pab, /* parent bridge to which child bridge is added */
  674. struct acpi_bridge *cab /* child bridge to add */
  675. )
  676. {
  677. struct acpi_bridge *lpab;
  678. struct acpi_bridge *lcab;
  679. lpab = find_acpi_bridge_by_bus(*head, pab->seg, pab->bus);
  680. if (!lpab) {
  681. if (!(pab->type & BRIDGE_TYPE_HOST))
  682. warn("PCI parent bridge s:b(%x:%x) not in list.\n", pab->seg, pab->bus);
  683. pab->next = *head;
  684. *head = pab;
  685. lpab = pab;
  686. }
  687. if ((cab->type & BRIDGE_TYPE_HOST) && (pab == cab))
  688. return;
  689. lcab = find_acpi_bridge_by_bus(*head, cab->seg, cab->bus);
  690. if (lcab) {
  691. if ((pab->bus != lcab->parent->bus) || (lcab->bus != cab->bus))
  692. err("PCI child bridge s:b(%x:%x) in list with diff parent.\n", cab->seg, cab->bus);
  693. return;
  694. } else
  695. lcab = cab;
  696. lcab->parent = lpab;
  697. lcab->next = lpab->child;
  698. lpab->child = lcab;
  699. }
  700. static acpi_status pciehprm_acpi_build_php_slots_callback(
  701. acpi_handle handle,
  702. u32 Level,
  703. void *context,
  704. void **retval
  705. )
  706. {
  707. ulong bus_num;
  708. ulong seg_num;
  709. ulong sun, adr;
  710. ulong padr = 0;
  711. acpi_handle phandle = NULL;
  712. struct acpi_bridge *pab = (struct acpi_bridge *)context;
  713. struct acpi_bridge *lab;
  714. acpi_status status;
  715. u8 *path_name = acpi_path_name(handle);
  716. /* get _SUN */
  717. status = acpi_evaluate_integer(handle, METHOD_NAME__SUN, NULL, &sun);
  718. switch(status) {
  719. case AE_NOT_FOUND:
  720. return AE_OK;
  721. default:
  722. if (ACPI_FAILURE(status)) {
  723. err("acpi_pciehprm:%s _SUN fail=0x%x\n", path_name, status);
  724. return status;
  725. }
  726. }
  727. /* get _ADR. _ADR must exist if _SUN exists */
  728. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
  729. if (ACPI_FAILURE(status)) {
  730. err("acpi_pciehprm:%s _ADR fail=0x%x\n", path_name, status);
  731. return status;
  732. }
  733. dbg("acpi_pciehprm:%s sun=0x%08x adr=0x%08x\n", path_name, (u32)sun, (u32)adr);
  734. status = acpi_get_parent(handle, &phandle);
  735. if (ACPI_FAILURE(status)) {
  736. err("acpi_pciehprm:%s get_parent fail=0x%x\n", path_name, status);
  737. return (status);
  738. }
  739. bus_num = pab->bus;
  740. seg_num = pab->seg;
  741. if (pab->bus == bus_num) {
  742. lab = pab;
  743. } else {
  744. dbg("WARN: pab is not parent\n");
  745. lab = find_acpi_bridge_by_bus(pab, seg_num, bus_num);
  746. if (!lab) {
  747. dbg("acpi_pciehprm: alloc new P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
  748. lab = (struct acpi_bridge *)kmalloc(sizeof(struct acpi_bridge), GFP_KERNEL);
  749. if (!lab) {
  750. err("acpi_pciehprm: alloc for ab fail\n");
  751. return AE_NO_MEMORY;
  752. }
  753. memset(lab, 0, sizeof(struct acpi_bridge));
  754. lab->handle = phandle;
  755. lab->pbus = pab->bus;
  756. lab->pdevice = (int)(padr >> 16) & 0xffff;
  757. lab->pfunction = (int)(padr & 0xffff);
  758. lab->bus = (int)bus_num;
  759. lab->scanned = 0;
  760. lab->type = BRIDGE_TYPE_P2P;
  761. pciehprm_acpi_register_a_bridge (&acpi_bridges_head, pab, lab);
  762. } else
  763. dbg("acpi_pciehprm: found P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
  764. }
  765. acpi_add_slot_to_php_slots(lab, (int)bus_num, handle, (u32)adr, (u32)sun);
  766. return (status);
  767. }
  768. static int pciehprm_acpi_build_php_slots(
  769. struct acpi_bridge *ab,
  770. u32 depth
  771. )
  772. {
  773. acpi_status status;
  774. u8 *path_name = acpi_path_name(ab->handle);
  775. /* Walk down this pci bridge to get _SUNs if any behind P2P */
  776. status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
  777. ab->handle,
  778. depth,
  779. pciehprm_acpi_build_php_slots_callback,
  780. ab,
  781. NULL );
  782. if (ACPI_FAILURE(status)) {
  783. dbg("acpi_pciehprm:%s walk for _SUN on pci bridge seg:bus(%x:%x) fail=0x%x\n", path_name, ab->seg, ab->bus, status);
  784. return -1;
  785. }
  786. return 0;
  787. }
  788. static void build_a_bridge(
  789. struct acpi_bridge *pab,
  790. struct acpi_bridge *ab
  791. )
  792. {
  793. u8 *path_name = acpi_path_name(ab->handle);
  794. pciehprm_acpi_register_a_bridge (&acpi_bridges_head, pab, ab);
  795. switch (ab->type) {
  796. case BRIDGE_TYPE_HOST:
  797. dbg("acpi_pciehprm: Registered PCI HOST Bridge(%02x) on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
  798. ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
  799. break;
  800. case BRIDGE_TYPE_P2P:
  801. dbg("acpi_pciehprm: Registered PCI P2P Bridge(%02x-%02x) on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
  802. ab->pbus, ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
  803. break;
  804. };
  805. /* build any immediate PHP slots under this pci bridge */
  806. pciehprm_acpi_build_php_slots(ab, 1);
  807. }
  808. static struct acpi_bridge * add_p2p_bridge(
  809. acpi_handle handle,
  810. struct acpi_bridge *pab, /* parent */
  811. ulong adr
  812. )
  813. {
  814. struct acpi_bridge *ab;
  815. struct pci_dev *pdev;
  816. ulong devnum, funcnum;
  817. u8 *path_name = acpi_path_name(handle);
  818. ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
  819. if (!ab) {
  820. err("acpi_pciehprm: alloc for ab fail\n");
  821. return NULL;
  822. }
  823. memset(ab, 0, sizeof(struct acpi_bridge));
  824. devnum = (adr >> 16) & 0xffff;
  825. funcnum = adr & 0xffff;
  826. pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
  827. if (!pdev || !pdev->subordinate) {
  828. err("acpi_pciehprm:%s is not a P2P Bridge\n", path_name);
  829. kfree(ab);
  830. return NULL;
  831. }
  832. ab->handle = handle;
  833. ab->seg = pab->seg;
  834. ab->pbus = pab->bus; /* or pdev->bus->number */
  835. ab->pdevice = devnum; /* or PCI_SLOT(pdev->devfn) */
  836. ab->pfunction = funcnum; /* or PCI_FUNC(pdev->devfn) */
  837. ab->bus = pdev->subordinate->number;
  838. ab->scanned = 0;
  839. ab->type = BRIDGE_TYPE_P2P;
  840. dbg("acpi_pciehprm: P2P(%x-%x) on pci=b:d:f(%x:%x:%x) acpi=b:d:f(%x:%x:%x) [%s]\n",
  841. pab->bus, ab->bus, pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  842. pab->bus, (u32)devnum, (u32)funcnum, path_name);
  843. build_a_bridge(pab, ab);
  844. return ab;
  845. }
  846. static acpi_status scan_p2p_bridge(
  847. acpi_handle handle,
  848. u32 Level,
  849. void *context,
  850. void **retval
  851. )
  852. {
  853. struct acpi_bridge *pab = (struct acpi_bridge *)context;
  854. struct acpi_bridge *ab;
  855. acpi_status status;
  856. ulong adr = 0;
  857. u8 *path_name = acpi_path_name(handle);
  858. ulong devnum, funcnum;
  859. struct pci_dev *pdev;
  860. /* get device, function */
  861. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
  862. if (ACPI_FAILURE(status)) {
  863. if (status != AE_NOT_FOUND)
  864. err("acpi_pciehprm:%s _ADR fail=0x%x\n", path_name, status);
  865. return AE_OK;
  866. }
  867. devnum = (adr >> 16) & 0xffff;
  868. funcnum = adr & 0xffff;
  869. pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
  870. if (!pdev)
  871. return AE_OK;
  872. if (!pdev->subordinate)
  873. return AE_OK;
  874. ab = add_p2p_bridge(handle, pab, adr);
  875. if (ab) {
  876. status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
  877. handle,
  878. (u32)1,
  879. scan_p2p_bridge,
  880. ab,
  881. NULL);
  882. if (ACPI_FAILURE(status))
  883. dbg("acpi_pciehprm:%s find_p2p fail=0x%x\n", path_name, status);
  884. }
  885. return AE_OK;
  886. }
  887. static struct acpi_bridge * add_host_bridge(
  888. acpi_handle handle,
  889. ulong segnum,
  890. ulong busnum
  891. )
  892. {
  893. ulong adr = 0;
  894. acpi_status status;
  895. struct acpi_bridge *ab;
  896. u8 *path_name = acpi_path_name(handle);
  897. /* get device, function: host br adr is always 0000 though. */
  898. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
  899. if (ACPI_FAILURE(status)) {
  900. err("acpi_pciehprm:%s _ADR fail=0x%x\n", path_name, status);
  901. return NULL;
  902. }
  903. dbg("acpi_pciehprm: ROOT PCI seg(0x%x)bus(0x%x)dev(0x%x)func(0x%x) [%s]\n", (u32)segnum,
  904. (u32)busnum, (u32)(adr >> 16) & 0xffff, (u32)adr & 0xffff, path_name);
  905. ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
  906. if (!ab) {
  907. err("acpi_pciehprm: alloc for ab fail\n");
  908. return NULL;
  909. }
  910. memset(ab, 0, sizeof(struct acpi_bridge));
  911. ab->handle = handle;
  912. ab->seg = (int)segnum;
  913. ab->bus = ab->pbus = (int)busnum;
  914. ab->pdevice = (int)(adr >> 16) & 0xffff;
  915. ab->pfunction = (int)(adr & 0xffff);
  916. ab->scanned = 0;
  917. ab->type = BRIDGE_TYPE_HOST;
  918. /* get root pci bridge's current resources */
  919. status = acpi_get_crs(ab);
  920. if (ACPI_FAILURE(status)) {
  921. err("acpi_pciehprm:%s evaluate _CRS fail=0x%x\n", path_name, status);
  922. kfree(ab);
  923. return NULL;
  924. }
  925. status = pci_osc_control_set (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
  926. if (ACPI_FAILURE(status)) {
  927. err("%s: status %x\n", __FUNCTION__, status);
  928. osc_run_status = (status == AE_NOT_FOUND) ? OSC_NOT_EXIST : OSC_RUN_FAILED;
  929. } else {
  930. osc_run_status = NC_RUN_SUCCESS;
  931. }
  932. dbg("%s: osc_run_status %x\n", __FUNCTION__, osc_run_status);
  933. build_a_bridge(ab, ab);
  934. return ab;
  935. }
  936. static acpi_status acpi_scan_from_root_pci_callback (
  937. acpi_handle handle,
  938. u32 Level,
  939. void *context,
  940. void **retval
  941. )
  942. {
  943. ulong segnum = 0;
  944. ulong busnum = 0;
  945. acpi_status status;
  946. struct acpi_bridge *ab;
  947. u8 *path_name = acpi_path_name(handle);
  948. /* get bus number of this pci root bridge */
  949. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &segnum);
  950. if (ACPI_FAILURE(status)) {
  951. if (status != AE_NOT_FOUND) {
  952. err("acpi_pciehprm:%s evaluate _SEG fail=0x%x\n", path_name, status);
  953. return status;
  954. }
  955. segnum = 0;
  956. }
  957. /* get bus number of this pci root bridge */
  958. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL, &busnum);
  959. if (ACPI_FAILURE(status)) {
  960. err("acpi_pciehprm:%s evaluate _BBN fail=0x%x\n", path_name, status);
  961. return (status);
  962. }
  963. ab = add_host_bridge(handle, segnum, busnum);
  964. if (ab) {
  965. status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
  966. handle,
  967. 1,
  968. scan_p2p_bridge,
  969. ab,
  970. NULL);
  971. if (ACPI_FAILURE(status))
  972. dbg("acpi_pciehprm:%s find_p2p fail=0x%x\n", path_name, status);
  973. }
  974. return AE_OK;
  975. }
  976. static int pciehprm_acpi_scan_pci (void)
  977. {
  978. acpi_status status;
  979. /*
  980. * TBD: traverse LDM device tree with the help of
  981. * unified ACPI augmented for php device population.
  982. */
  983. status = acpi_get_devices ( PCI_ROOT_HID_STRING,
  984. acpi_scan_from_root_pci_callback,
  985. NULL,
  986. NULL );
  987. if (ACPI_FAILURE(status)) {
  988. err("acpi_pciehprm:get_device PCI ROOT HID fail=0x%x\n", status);
  989. return -1;
  990. }
  991. return 0;
  992. }
  993. int pciehprm_init(enum php_ctlr_type ctlr_type)
  994. {
  995. int rc;
  996. if (ctlr_type != PCI)
  997. return -ENODEV;
  998. dbg("pciehprm ACPI init <enter>\n");
  999. acpi_bridges_head = NULL;
  1000. /* construct PCI bus:device tree of acpi_handles */
  1001. rc = pciehprm_acpi_scan_pci();
  1002. if (rc)
  1003. return rc;
  1004. if ((oshp_run_status != NC_RUN_SUCCESS) && (osc_run_status != NC_RUN_SUCCESS)) {
  1005. err("Fails to gain control of native hot-plug\n");
  1006. rc = -ENODEV;
  1007. }
  1008. dbg("pciehprm ACPI init %s\n", (rc)?"fail":"success");
  1009. return rc;
  1010. }
  1011. static void free_a_slot(struct acpi_php_slot *aps)
  1012. {
  1013. dbg(" free a php func of slot(0x%02x) on PCI b:d:f=0x%02x:%02x:%02x\n", aps->sun, aps->bus, aps->dev, aps->fun);
  1014. free_pci_resource (aps->io_head);
  1015. free_pci_resource (aps->bus_head);
  1016. free_pci_resource (aps->mem_head);
  1017. free_pci_resource (aps->p_mem_head);
  1018. kfree(aps);
  1019. }
  1020. static void free_a_bridge( struct acpi_bridge *ab)
  1021. {
  1022. struct acpi_php_slot *aps, *next;
  1023. switch (ab->type) {
  1024. case BRIDGE_TYPE_HOST:
  1025. dbg("Free ACPI PCI HOST Bridge(%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
  1026. ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
  1027. break;
  1028. case BRIDGE_TYPE_P2P:
  1029. dbg("Free ACPI PCI P2P Bridge(%x-%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
  1030. ab->pbus, ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
  1031. break;
  1032. };
  1033. /* free slots first */
  1034. for (aps = ab->slots; aps; aps = next) {
  1035. next = aps->next;
  1036. free_a_slot(aps);
  1037. }
  1038. free_pci_resource (ab->io_head);
  1039. free_pci_resource (ab->tio_head);
  1040. free_pci_resource (ab->bus_head);
  1041. free_pci_resource (ab->tbus_head);
  1042. free_pci_resource (ab->mem_head);
  1043. free_pci_resource (ab->tmem_head);
  1044. free_pci_resource (ab->p_mem_head);
  1045. free_pci_resource (ab->tp_mem_head);
  1046. kfree(ab);
  1047. }
  1048. static void pciehprm_free_bridges ( struct acpi_bridge *ab)
  1049. {
  1050. if (!ab)
  1051. return;
  1052. if (ab->child)
  1053. pciehprm_free_bridges (ab->child);
  1054. if (ab->next)
  1055. pciehprm_free_bridges (ab->next);
  1056. free_a_bridge(ab);
  1057. }
  1058. void pciehprm_cleanup(void)
  1059. {
  1060. pciehprm_free_bridges (acpi_bridges_head);
  1061. }
  1062. static int get_number_of_slots (
  1063. struct acpi_bridge *ab,
  1064. int selfonly
  1065. )
  1066. {
  1067. struct acpi_php_slot *aps;
  1068. int prev_slot = -1;
  1069. int slot_num = 0;
  1070. for ( aps = ab->slots; aps; aps = aps->next)
  1071. if (aps->dev != prev_slot) {
  1072. prev_slot = aps->dev;
  1073. slot_num++;
  1074. }
  1075. if (ab->child)
  1076. slot_num += get_number_of_slots (ab->child, 0);
  1077. if (selfonly)
  1078. return slot_num;
  1079. if (ab->next)
  1080. slot_num += get_number_of_slots (ab->next, 0);
  1081. return slot_num;
  1082. }
  1083. static int print_acpi_resources (struct acpi_bridge *ab)
  1084. {
  1085. struct acpi_php_slot *aps;
  1086. int i;
  1087. switch (ab->type) {
  1088. case BRIDGE_TYPE_HOST:
  1089. dbg("PCI HOST Bridge (%x) [%s]\n", ab->bus, acpi_path_name(ab->handle));
  1090. break;
  1091. case BRIDGE_TYPE_P2P:
  1092. dbg("PCI P2P Bridge (%x-%x) [%s]\n", ab->pbus, ab->bus, acpi_path_name(ab->handle));
  1093. break;
  1094. };
  1095. print_pci_resources (ab);
  1096. for ( i = -1, aps = ab->slots; aps; aps = aps->next) {
  1097. if (aps->dev == i)
  1098. continue;
  1099. dbg(" Slot sun(%x) s:b:d:f(%02x:%02x:%02x:%02x)\n", aps->sun, aps->seg, aps->bus, aps->dev, aps->fun);
  1100. print_slot_resources(aps);
  1101. i = aps->dev;
  1102. }
  1103. if (ab->child)
  1104. print_acpi_resources (ab->child);
  1105. if (ab->next)
  1106. print_acpi_resources (ab->next);
  1107. return 0;
  1108. }
  1109. int pciehprm_print_pirt(void)
  1110. {
  1111. dbg("PCIEHPRM ACPI Slots\n");
  1112. if (acpi_bridges_head)
  1113. print_acpi_resources (acpi_bridges_head);
  1114. return 0;
  1115. }
  1116. static struct acpi_php_slot * get_acpi_slot (
  1117. struct acpi_bridge *ab,
  1118. u32 sun
  1119. )
  1120. {
  1121. struct acpi_php_slot *aps = NULL;
  1122. for ( aps = ab->slots; aps; aps = aps->next)
  1123. if (aps->sun == sun)
  1124. return aps;
  1125. if (!aps && ab->child) {
  1126. aps = (struct acpi_php_slot *)get_acpi_slot (ab->child, sun);
  1127. if (aps)
  1128. return aps;
  1129. }
  1130. if (!aps && ab->next) {
  1131. aps = (struct acpi_php_slot *)get_acpi_slot (ab->next, sun);
  1132. if (aps)
  1133. return aps;
  1134. }
  1135. return aps;
  1136. }
  1137. #if 0
  1138. void * pciehprm_get_slot(struct slot *slot)
  1139. {
  1140. struct acpi_bridge *ab = acpi_bridges_head;
  1141. struct acpi_php_slot *aps = get_acpi_slot (ab, slot->number);
  1142. aps->slot = slot;
  1143. dbg("Got acpi slot sun(%x): s:b:d:f(%x:%x:%x:%x)\n", aps->sun, aps->seg, aps->bus, aps->dev, aps->fun);
  1144. return (void *)aps;
  1145. }
  1146. #endif
  1147. static void pciehprm_dump_func_res( struct pci_func *fun)
  1148. {
  1149. struct pci_func *func = fun;
  1150. if (func->bus_head) {
  1151. dbg(": BUS Resources:\n");
  1152. print_pci_resource (func->bus_head);
  1153. }
  1154. if (func->io_head) {
  1155. dbg(": IO Resources:\n");
  1156. print_pci_resource (func->io_head);
  1157. }
  1158. if (func->mem_head) {
  1159. dbg(": MEM Resources:\n");
  1160. print_pci_resource (func->mem_head);
  1161. }
  1162. if (func->p_mem_head) {
  1163. dbg(": PMEM Resources:\n");
  1164. print_pci_resource (func->p_mem_head);
  1165. }
  1166. }
  1167. static void pciehprm_dump_ctrl_res( struct controller *ctlr)
  1168. {
  1169. struct controller *ctrl = ctlr;
  1170. if (ctrl->bus_head) {
  1171. dbg(": BUS Resources:\n");
  1172. print_pci_resource (ctrl->bus_head);
  1173. }
  1174. if (ctrl->io_head) {
  1175. dbg(": IO Resources:\n");
  1176. print_pci_resource (ctrl->io_head);
  1177. }
  1178. if (ctrl->mem_head) {
  1179. dbg(": MEM Resources:\n");
  1180. print_pci_resource (ctrl->mem_head);
  1181. }
  1182. if (ctrl->p_mem_head) {
  1183. dbg(": PMEM Resources:\n");
  1184. print_pci_resource (ctrl->p_mem_head);
  1185. }
  1186. }
  1187. static int pciehprm_get_used_resources (
  1188. struct controller *ctrl,
  1189. struct pci_func *func
  1190. )
  1191. {
  1192. return pciehp_save_used_resources (ctrl, func, !DISABLE_CARD);
  1193. }
  1194. static int configure_existing_function(
  1195. struct controller *ctrl,
  1196. struct pci_func *func
  1197. )
  1198. {
  1199. int rc;
  1200. /* see how much resources the func has used. */
  1201. rc = pciehprm_get_used_resources (ctrl, func);
  1202. if (!rc) {
  1203. /* subtract the resources used by the func from ctrl resources */
  1204. rc = pciehprm_delete_resources (&ctrl->bus_head, func->bus_head);
  1205. rc |= pciehprm_delete_resources (&ctrl->io_head, func->io_head);
  1206. rc |= pciehprm_delete_resources (&ctrl->mem_head, func->mem_head);
  1207. rc |= pciehprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
  1208. if (rc)
  1209. warn("aCEF: cannot del used resources\n");
  1210. } else
  1211. err("aCEF: cannot get used resources\n");
  1212. return rc;
  1213. }
  1214. static int bind_pci_resources_to_slots ( struct controller *ctrl)
  1215. {
  1216. struct pci_func *func, new_func;
  1217. int busn = ctrl->slot_bus;
  1218. int devn, funn;
  1219. u32 vid;
  1220. for (devn = 0; devn < 32; devn++) {
  1221. for (funn = 0; funn < 8; funn++) {
  1222. /*
  1223. if (devn == ctrl->device && funn == ctrl->function)
  1224. continue;
  1225. */
  1226. /* find out if this entry is for an occupied slot */
  1227. vid = 0xFFFFFFFF;
  1228. pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
  1229. if (vid != 0xFFFFFFFF) {
  1230. dbg("%s: vid = %x\n", __FUNCTION__, vid);
  1231. func = pciehp_slot_find(busn, devn, funn);
  1232. if (!func) {
  1233. memset(&new_func, 0, sizeof(struct pci_func));
  1234. new_func.bus = busn;
  1235. new_func.device = devn;
  1236. new_func.function = funn;
  1237. new_func.is_a_board = 1;
  1238. configure_existing_function(ctrl, &new_func);
  1239. pciehprm_dump_func_res(&new_func);
  1240. } else {
  1241. configure_existing_function(ctrl, func);
  1242. pciehprm_dump_func_res(func);
  1243. }
  1244. dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
  1245. }
  1246. }
  1247. }
  1248. return 0;
  1249. }
  1250. static int bind_pci_resources(
  1251. struct controller *ctrl,
  1252. struct acpi_bridge *ab
  1253. )
  1254. {
  1255. int status = 0;
  1256. if (ab->bus_head) {
  1257. dbg("bapr: BUS Resources add on PCI 0x%x\n", ab->bus);
  1258. status = pciehprm_add_resources (&ctrl->bus_head, ab->bus_head);
  1259. if (pciehprm_delete_resources (&ab->bus_head, ctrl->bus_head))
  1260. warn("bapr: cannot sub BUS Resource on PCI 0x%x\n", ab->bus);
  1261. if (status) {
  1262. err("bapr: BUS Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
  1263. return status;
  1264. }
  1265. } else
  1266. info("bapr: No BUS Resource on PCI 0x%x.\n", ab->bus);
  1267. if (ab->io_head) {
  1268. dbg("bapr: IO Resources add on PCI 0x%x\n", ab->bus);
  1269. status = pciehprm_add_resources (&ctrl->io_head, ab->io_head);
  1270. if (pciehprm_delete_resources (&ab->io_head, ctrl->io_head))
  1271. warn("bapr: cannot sub IO Resource on PCI 0x%x\n", ab->bus);
  1272. if (status) {
  1273. err("bapr: IO Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
  1274. return status;
  1275. }
  1276. } else
  1277. info("bapr: No IO Resource on PCI 0x%x.\n", ab->bus);
  1278. if (ab->mem_head) {
  1279. dbg("bapr: MEM Resources add on PCI 0x%x\n", ab->bus);
  1280. status = pciehprm_add_resources (&ctrl->mem_head, ab->mem_head);
  1281. if (pciehprm_delete_resources (&ab->mem_head, ctrl->mem_head))
  1282. warn("bapr: cannot sub MEM Resource on PCI 0x%x\n", ab->bus);
  1283. if (status) {
  1284. err("bapr: MEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
  1285. return status;
  1286. }
  1287. } else
  1288. info("bapr: No MEM Resource on PCI 0x%x.\n", ab->bus);
  1289. if (ab->p_mem_head) {
  1290. dbg("bapr: PMEM Resources add on PCI 0x%x\n", ab->bus);
  1291. status = pciehprm_add_resources (&ctrl->p_mem_head, ab->p_mem_head);
  1292. if (pciehprm_delete_resources (&ab->p_mem_head, ctrl->p_mem_head))
  1293. warn("bapr: cannot sub PMEM Resource on PCI 0x%x\n", ab->bus);
  1294. if (status) {
  1295. err("bapr: PMEM Resource add on PCI 0x%x: fail=0x%x\n", ab->bus, status);
  1296. return status;
  1297. }
  1298. } else
  1299. info("bapr: No PMEM Resource on PCI 0x%x.\n", ab->bus);
  1300. return status;
  1301. }
  1302. static int no_pci_resources( struct acpi_bridge *ab)
  1303. {
  1304. return !(ab->p_mem_head || ab->mem_head || ab->io_head || ab->bus_head);
  1305. }
  1306. static int find_pci_bridge_resources (
  1307. struct controller *ctrl,
  1308. struct acpi_bridge *ab
  1309. )
  1310. {
  1311. int rc = 0;
  1312. struct pci_func func;
  1313. memset(&func, 0, sizeof(struct pci_func));
  1314. func.bus = ab->pbus;
  1315. func.device = ab->pdevice;
  1316. func.function = ab->pfunction;
  1317. func.is_a_board = 1;
  1318. /* Get used resources for this PCI bridge */
  1319. rc = pciehp_save_used_resources (ctrl, &func, !DISABLE_CARD);
  1320. ab->io_head = func.io_head;
  1321. ab->mem_head = func.mem_head;
  1322. ab->p_mem_head = func.p_mem_head;
  1323. ab->bus_head = func.bus_head;
  1324. if (ab->bus_head)
  1325. pciehprm_delete_resource(&ab->bus_head, ctrl->pci_dev->subordinate->number, 1);
  1326. return rc;
  1327. }
  1328. static int get_pci_resources_from_bridge(
  1329. struct controller *ctrl,
  1330. struct acpi_bridge *ab
  1331. )
  1332. {
  1333. int rc = 0;
  1334. dbg("grfb: Get Resources for PCI 0x%x from actual PCI bridge 0x%x.\n", ctrl->bus, ab->bus);
  1335. rc = find_pci_bridge_resources (ctrl, ab);
  1336. pciehp_resource_sort_and_combine(&ab->bus_head);
  1337. pciehp_resource_sort_and_combine(&ab->io_head);
  1338. pciehp_resource_sort_and_combine(&ab->mem_head);
  1339. pciehp_resource_sort_and_combine(&ab->p_mem_head);
  1340. pciehprm_add_resources (&ab->tbus_head, ab->bus_head);
  1341. pciehprm_add_resources (&ab->tio_head, ab->io_head);
  1342. pciehprm_add_resources (&ab->tmem_head, ab->mem_head);
  1343. pciehprm_add_resources (&ab->tp_mem_head, ab->p_mem_head);
  1344. return rc;
  1345. }
  1346. static int get_pci_resources(
  1347. struct controller *ctrl,
  1348. struct acpi_bridge *ab
  1349. )
  1350. {
  1351. int rc = 0;
  1352. if (no_pci_resources(ab)) {
  1353. dbg("spbr:PCI 0x%x has no resources. Get parent resources.\n", ab->bus);
  1354. rc = get_pci_resources_from_bridge(ctrl, ab);
  1355. }
  1356. return rc;
  1357. }
  1358. /*
  1359. * Get resources for this ctrl.
  1360. * 1. get total resources from ACPI _CRS or bridge (this ctrl)
  1361. * 2. find used resources of existing adapters
  1362. * 3. subtract used resources from total resources
  1363. */
  1364. int pciehprm_find_available_resources( struct controller *ctrl)
  1365. {
  1366. int rc = 0;
  1367. struct acpi_bridge *ab;
  1368. ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->pci_dev->subordinate->number);
  1369. if (!ab) {
  1370. err("pfar:cannot locate acpi bridge of PCI 0x%x.\n", ctrl->pci_dev->subordinate->number);
  1371. return -1;
  1372. }
  1373. if (no_pci_resources(ab)) {
  1374. rc = get_pci_resources(ctrl, ab);
  1375. if (rc) {
  1376. err("pfar:cannot get pci resources of PCI 0x%x.\n", ctrl->pci_dev->subordinate->number);
  1377. return -1;
  1378. }
  1379. }
  1380. rc = bind_pci_resources(ctrl, ab);
  1381. dbg("pfar:pre-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
  1382. pciehprm_dump_ctrl_res(ctrl);
  1383. bind_pci_resources_to_slots (ctrl);
  1384. dbg("pfar:post-Bind PCI 0x%x Ctrl Resource Dump\n", ctrl->pci_dev->subordinate->number);
  1385. pciehprm_dump_ctrl_res(ctrl);
  1386. return rc;
  1387. }
  1388. int pciehprm_set_hpp(
  1389. struct controller *ctrl,
  1390. struct pci_func *func,
  1391. u8 card_type
  1392. )
  1393. {
  1394. struct acpi_bridge *ab;
  1395. struct pci_bus lpci_bus, *pci_bus;
  1396. int rc = 0;
  1397. unsigned int devfn;
  1398. u8 cls= 0x08; /* default cache line size */
  1399. u8 lt = 0x40; /* default latency timer */
  1400. u8 ep = 0;
  1401. u8 es = 0;
  1402. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  1403. pci_bus = &lpci_bus;
  1404. pci_bus->number = func->bus;
  1405. devfn = PCI_DEVFN(func->device, func->function);
  1406. ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->bus);
  1407. if (ab) {
  1408. if (ab->_hpp) {
  1409. lt = (u8)ab->_hpp->latency_timer;
  1410. cls = (u8)ab->_hpp->cache_line_size;
  1411. ep = (u8)ab->_hpp->enable_perr;
  1412. es = (u8)ab->_hpp->enable_serr;
  1413. } else
  1414. dbg("_hpp: no _hpp for B/D/F=%#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
  1415. } else
  1416. dbg("_hpp: no acpi bridge for B/D/F = %#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
  1417. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  1418. /* set subordinate Latency Timer */
  1419. rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, lt);
  1420. }
  1421. /* set base Latency Timer */
  1422. rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, lt);
  1423. dbg(" set latency timer =0x%02x: %x\n", lt, rc);
  1424. rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, cls);
  1425. dbg(" set cache_line_size=0x%02x: %x\n", cls, rc);
  1426. return rc;
  1427. }
  1428. void pciehprm_enable_card(
  1429. struct controller *ctrl,
  1430. struct pci_func *func,
  1431. u8 card_type)
  1432. {
  1433. u16 command, cmd, bcommand, bcmd;
  1434. struct pci_bus lpci_bus, *pci_bus;
  1435. struct acpi_bridge *ab;
  1436. unsigned int devfn;
  1437. int rc;
  1438. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  1439. pci_bus = &lpci_bus;
  1440. pci_bus->number = func->bus;
  1441. devfn = PCI_DEVFN(func->device, func->function);
  1442. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &cmd);
  1443. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  1444. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcmd);
  1445. }
  1446. command = cmd | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
  1447. | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  1448. bcommand = bcmd | PCI_BRIDGE_CTL_NO_ISA;
  1449. ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->bus);
  1450. if (ab) {
  1451. if (ab->_hpp) {
  1452. if (ab->_hpp->enable_perr) {
  1453. command |= PCI_COMMAND_PARITY;
  1454. bcommand |= PCI_BRIDGE_CTL_PARITY;
  1455. } else {
  1456. command &= ~PCI_COMMAND_PARITY;
  1457. bcommand &= ~PCI_BRIDGE_CTL_PARITY;
  1458. }
  1459. if (ab->_hpp->enable_serr) {
  1460. command |= PCI_COMMAND_SERR;
  1461. bcommand |= PCI_BRIDGE_CTL_SERR;
  1462. } else {
  1463. command &= ~PCI_COMMAND_SERR;
  1464. bcommand &= ~PCI_BRIDGE_CTL_SERR;
  1465. }
  1466. } else
  1467. dbg("no _hpp for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
  1468. } else
  1469. dbg("no acpi bridge for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
  1470. if (command != cmd) {
  1471. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
  1472. }
  1473. if ((card_type == PCI_HEADER_TYPE_BRIDGE) && (bcommand != bcmd)) {
  1474. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
  1475. }
  1476. }