shpchprm_acpi.c 43 KB

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