pci-sysfs.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2002-2004 Greg Kroah-Hartman <greg@kroah.com>
  4. * (C) Copyright 2002-2004 IBM Corp.
  5. * (C) Copyright 2003 Matthew Wilcox
  6. * (C) Copyright 2003 Hewlett-Packard
  7. * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
  8. * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
  9. *
  10. * File attributes for PCI devices
  11. *
  12. * Modeled after usb's driverfs.c
  13. */
  14. #include <linux/bitfield.h>
  15. #include <linux/cleanup.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/pci.h>
  19. #include <linux/stat.h>
  20. #include <linux/export.h>
  21. #include <linux/topology.h>
  22. #include <linux/mm.h>
  23. #include <linux/fs.h>
  24. #include <linux/capability.h>
  25. #include <linux/security.h>
  26. #include <linux/slab.h>
  27. #include <linux/vgaarb.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/msi.h>
  30. #include <linux/of.h>
  31. #include <linux/aperture.h>
  32. #include <linux/unaligned.h>
  33. #include "pci.h"
  34. #ifndef ARCH_PCI_DEV_GROUPS
  35. #define ARCH_PCI_DEV_GROUPS
  36. #endif
  37. static int sysfs_initialized; /* = 0 */
  38. /* show configuration fields */
  39. #define pci_config_attr(field, format_string) \
  40. static ssize_t \
  41. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  42. { \
  43. struct pci_dev *pdev; \
  44. \
  45. pdev = to_pci_dev(dev); \
  46. return sysfs_emit(buf, format_string, pdev->field); \
  47. } \
  48. static DEVICE_ATTR_RO(field)
  49. pci_config_attr(vendor, "0x%04x\n");
  50. pci_config_attr(device, "0x%04x\n");
  51. pci_config_attr(subsystem_vendor, "0x%04x\n");
  52. pci_config_attr(subsystem_device, "0x%04x\n");
  53. pci_config_attr(revision, "0x%02x\n");
  54. pci_config_attr(class, "0x%06x\n");
  55. static ssize_t irq_show(struct device *dev,
  56. struct device_attribute *attr,
  57. char *buf)
  58. {
  59. struct pci_dev *pdev = to_pci_dev(dev);
  60. #ifdef CONFIG_PCI_MSI
  61. /*
  62. * For MSI, show the first MSI IRQ; for all other cases including
  63. * MSI-X, show the legacy INTx IRQ.
  64. */
  65. if (pdev->msi_enabled)
  66. return sysfs_emit(buf, "%u\n", pci_irq_vector(pdev, 0));
  67. #endif
  68. return sysfs_emit(buf, "%u\n", pdev->irq);
  69. }
  70. static DEVICE_ATTR_RO(irq);
  71. static ssize_t broken_parity_status_show(struct device *dev,
  72. struct device_attribute *attr,
  73. char *buf)
  74. {
  75. struct pci_dev *pdev = to_pci_dev(dev);
  76. return sysfs_emit(buf, "%u\n", pdev->broken_parity_status);
  77. }
  78. static ssize_t broken_parity_status_store(struct device *dev,
  79. struct device_attribute *attr,
  80. const char *buf, size_t count)
  81. {
  82. struct pci_dev *pdev = to_pci_dev(dev);
  83. unsigned long val;
  84. if (kstrtoul(buf, 0, &val) < 0)
  85. return -EINVAL;
  86. pdev->broken_parity_status = !!val;
  87. return count;
  88. }
  89. static DEVICE_ATTR_RW(broken_parity_status);
  90. static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
  91. struct device_attribute *attr, char *buf)
  92. {
  93. const struct cpumask *mask;
  94. #ifdef CONFIG_NUMA
  95. if (dev_to_node(dev) == NUMA_NO_NODE)
  96. mask = cpu_online_mask;
  97. else
  98. mask = cpumask_of_node(dev_to_node(dev));
  99. #else
  100. mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
  101. #endif
  102. return cpumap_print_to_pagebuf(list, buf, mask);
  103. }
  104. static ssize_t local_cpus_show(struct device *dev,
  105. struct device_attribute *attr, char *buf)
  106. {
  107. return pci_dev_show_local_cpu(dev, false, attr, buf);
  108. }
  109. static DEVICE_ATTR_RO(local_cpus);
  110. static ssize_t local_cpulist_show(struct device *dev,
  111. struct device_attribute *attr, char *buf)
  112. {
  113. return pci_dev_show_local_cpu(dev, true, attr, buf);
  114. }
  115. static DEVICE_ATTR_RO(local_cpulist);
  116. /*
  117. * PCI Bus Class Devices
  118. */
  119. static ssize_t cpuaffinity_show(struct device *dev,
  120. struct device_attribute *attr, char *buf)
  121. {
  122. const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
  123. return cpumap_print_to_pagebuf(false, buf, cpumask);
  124. }
  125. static DEVICE_ATTR_RO(cpuaffinity);
  126. static ssize_t cpulistaffinity_show(struct device *dev,
  127. struct device_attribute *attr, char *buf)
  128. {
  129. const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
  130. return cpumap_print_to_pagebuf(true, buf, cpumask);
  131. }
  132. static DEVICE_ATTR_RO(cpulistaffinity);
  133. static ssize_t power_state_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct pci_dev *pdev = to_pci_dev(dev);
  137. return sysfs_emit(buf, "%s\n", pci_power_name(pdev->current_state));
  138. }
  139. static DEVICE_ATTR_RO(power_state);
  140. /* show resources */
  141. static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
  142. char *buf)
  143. {
  144. struct pci_dev *pci_dev = to_pci_dev(dev);
  145. int i;
  146. int max;
  147. resource_size_t start, end;
  148. size_t len = 0;
  149. if (pci_dev->subordinate)
  150. max = DEVICE_COUNT_RESOURCE;
  151. else
  152. max = PCI_BRIDGE_RESOURCES;
  153. for (i = 0; i < max; i++) {
  154. struct resource *res = &pci_dev->resource[i];
  155. struct resource zerores = {};
  156. /* For backwards compatibility */
  157. if (pci_resource_is_bridge_win(i) &&
  158. res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
  159. res = &zerores;
  160. pci_resource_to_user(pci_dev, i, res, &start, &end);
  161. len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n",
  162. (unsigned long long)start,
  163. (unsigned long long)end,
  164. (unsigned long long)res->flags);
  165. }
  166. return len;
  167. }
  168. static DEVICE_ATTR_RO(resource);
  169. static ssize_t max_link_speed_show(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct pci_dev *pdev = to_pci_dev(dev);
  173. return sysfs_emit(buf, "%s\n",
  174. pci_speed_string(pcie_get_speed_cap(pdev)));
  175. }
  176. static DEVICE_ATTR_RO(max_link_speed);
  177. static ssize_t max_link_width_show(struct device *dev,
  178. struct device_attribute *attr, char *buf)
  179. {
  180. struct pci_dev *pdev = to_pci_dev(dev);
  181. ssize_t ret;
  182. /* We read PCI_EXP_LNKCAP, so we need the device to be accessible. */
  183. pci_config_pm_runtime_get(pdev);
  184. ret = sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev));
  185. pci_config_pm_runtime_put(pdev);
  186. return ret;
  187. }
  188. static DEVICE_ATTR_RO(max_link_width);
  189. static ssize_t current_link_speed_show(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. struct pci_dev *pci_dev = to_pci_dev(dev);
  193. u16 linkstat;
  194. int err;
  195. enum pci_bus_speed speed;
  196. pci_config_pm_runtime_get(pci_dev);
  197. err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
  198. pci_config_pm_runtime_put(pci_dev);
  199. if (err)
  200. return -EINVAL;
  201. speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS];
  202. return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
  203. }
  204. static DEVICE_ATTR_RO(current_link_speed);
  205. static ssize_t current_link_width_show(struct device *dev,
  206. struct device_attribute *attr, char *buf)
  207. {
  208. struct pci_dev *pci_dev = to_pci_dev(dev);
  209. u16 linkstat;
  210. int err;
  211. pci_config_pm_runtime_get(pci_dev);
  212. err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
  213. pci_config_pm_runtime_put(pci_dev);
  214. if (err)
  215. return -EINVAL;
  216. return sysfs_emit(buf, "%u\n", FIELD_GET(PCI_EXP_LNKSTA_NLW, linkstat));
  217. }
  218. static DEVICE_ATTR_RO(current_link_width);
  219. static ssize_t secondary_bus_number_show(struct device *dev,
  220. struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct pci_dev *pci_dev = to_pci_dev(dev);
  224. u8 sec_bus;
  225. int err;
  226. pci_config_pm_runtime_get(pci_dev);
  227. err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
  228. pci_config_pm_runtime_put(pci_dev);
  229. if (err)
  230. return -EINVAL;
  231. return sysfs_emit(buf, "%u\n", sec_bus);
  232. }
  233. static DEVICE_ATTR_RO(secondary_bus_number);
  234. static ssize_t subordinate_bus_number_show(struct device *dev,
  235. struct device_attribute *attr,
  236. char *buf)
  237. {
  238. struct pci_dev *pci_dev = to_pci_dev(dev);
  239. u8 sub_bus;
  240. int err;
  241. pci_config_pm_runtime_get(pci_dev);
  242. err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
  243. pci_config_pm_runtime_put(pci_dev);
  244. if (err)
  245. return -EINVAL;
  246. return sysfs_emit(buf, "%u\n", sub_bus);
  247. }
  248. static DEVICE_ATTR_RO(subordinate_bus_number);
  249. static ssize_t ari_enabled_show(struct device *dev,
  250. struct device_attribute *attr,
  251. char *buf)
  252. {
  253. struct pci_dev *pci_dev = to_pci_dev(dev);
  254. return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
  255. }
  256. static DEVICE_ATTR_RO(ari_enabled);
  257. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  258. char *buf)
  259. {
  260. struct pci_dev *pci_dev = to_pci_dev(dev);
  261. return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
  262. pci_dev->vendor, pci_dev->device,
  263. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  264. (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
  265. (u8)(pci_dev->class));
  266. }
  267. static DEVICE_ATTR_RO(modalias);
  268. static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
  269. const char *buf, size_t count)
  270. {
  271. struct pci_dev *pdev = to_pci_dev(dev);
  272. unsigned long val;
  273. ssize_t result = 0;
  274. /* this can crash the machine when done on the "wrong" device */
  275. if (!capable(CAP_SYS_ADMIN))
  276. return -EPERM;
  277. if (kstrtoul(buf, 0, &val) < 0)
  278. return -EINVAL;
  279. device_lock(dev);
  280. if (dev->driver)
  281. result = -EBUSY;
  282. else if (val)
  283. result = pci_enable_device(pdev);
  284. else if (pci_is_enabled(pdev))
  285. pci_disable_device(pdev);
  286. else
  287. result = -EIO;
  288. device_unlock(dev);
  289. return result < 0 ? result : count;
  290. }
  291. static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
  292. char *buf)
  293. {
  294. struct pci_dev *pdev;
  295. pdev = to_pci_dev(dev);
  296. return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt));
  297. }
  298. static DEVICE_ATTR_RW(enable);
  299. #ifdef CONFIG_NUMA
  300. static ssize_t numa_node_store(struct device *dev,
  301. struct device_attribute *attr, const char *buf,
  302. size_t count)
  303. {
  304. struct pci_dev *pdev = to_pci_dev(dev);
  305. int node;
  306. if (!capable(CAP_SYS_ADMIN))
  307. return -EPERM;
  308. if (kstrtoint(buf, 0, &node) < 0)
  309. return -EINVAL;
  310. if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES)
  311. return -EINVAL;
  312. if (node != NUMA_NO_NODE && !node_online(node))
  313. return -EINVAL;
  314. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  315. pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.",
  316. node);
  317. dev->numa_node = node;
  318. return count;
  319. }
  320. static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
  321. char *buf)
  322. {
  323. return sysfs_emit(buf, "%d\n", dev->numa_node);
  324. }
  325. static DEVICE_ATTR_RW(numa_node);
  326. #endif
  327. static ssize_t dma_mask_bits_show(struct device *dev,
  328. struct device_attribute *attr, char *buf)
  329. {
  330. struct pci_dev *pdev = to_pci_dev(dev);
  331. return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask));
  332. }
  333. static DEVICE_ATTR_RO(dma_mask_bits);
  334. static ssize_t consistent_dma_mask_bits_show(struct device *dev,
  335. struct device_attribute *attr,
  336. char *buf)
  337. {
  338. return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask));
  339. }
  340. static DEVICE_ATTR_RO(consistent_dma_mask_bits);
  341. static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
  342. char *buf)
  343. {
  344. struct pci_dev *pdev = to_pci_dev(dev);
  345. struct pci_bus *subordinate = pdev->subordinate;
  346. return sysfs_emit(buf, "%u\n", subordinate ?
  347. !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  348. : !pdev->no_msi);
  349. }
  350. static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
  351. const char *buf, size_t count)
  352. {
  353. struct pci_dev *pdev = to_pci_dev(dev);
  354. struct pci_bus *subordinate = pdev->subordinate;
  355. unsigned long val;
  356. if (!capable(CAP_SYS_ADMIN))
  357. return -EPERM;
  358. if (kstrtoul(buf, 0, &val) < 0)
  359. return -EINVAL;
  360. /*
  361. * "no_msi" and "bus_flags" only affect what happens when a driver
  362. * requests MSI or MSI-X. They don't affect any drivers that have
  363. * already requested MSI or MSI-X.
  364. */
  365. if (!subordinate) {
  366. pdev->no_msi = !val;
  367. pci_info(pdev, "MSI/MSI-X %s for future drivers\n",
  368. val ? "allowed" : "disallowed");
  369. return count;
  370. }
  371. if (val)
  372. subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI;
  373. else
  374. subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI;
  375. dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n",
  376. val ? "allowed" : "disallowed");
  377. return count;
  378. }
  379. static DEVICE_ATTR_RW(msi_bus);
  380. static ssize_t rescan_store(const struct bus_type *bus, const char *buf, size_t count)
  381. {
  382. unsigned long val;
  383. struct pci_bus *b = NULL;
  384. if (kstrtoul(buf, 0, &val) < 0)
  385. return -EINVAL;
  386. if (val) {
  387. pci_lock_rescan_remove();
  388. while ((b = pci_find_next_bus(b)) != NULL)
  389. pci_rescan_bus(b);
  390. pci_unlock_rescan_remove();
  391. }
  392. return count;
  393. }
  394. static BUS_ATTR_WO(rescan);
  395. static struct attribute *pci_bus_attrs[] = {
  396. &bus_attr_rescan.attr,
  397. NULL,
  398. };
  399. static const struct attribute_group pci_bus_group = {
  400. .attrs = pci_bus_attrs,
  401. };
  402. const struct attribute_group *pci_bus_groups[] = {
  403. &pci_bus_group,
  404. NULL,
  405. };
  406. static ssize_t dev_rescan_store(struct device *dev,
  407. struct device_attribute *attr, const char *buf,
  408. size_t count)
  409. {
  410. unsigned long val;
  411. struct pci_dev *pdev = to_pci_dev(dev);
  412. if (kstrtoul(buf, 0, &val) < 0)
  413. return -EINVAL;
  414. if (val) {
  415. pci_lock_rescan_remove();
  416. pci_rescan_bus(pdev->bus);
  417. pci_unlock_rescan_remove();
  418. }
  419. return count;
  420. }
  421. static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL,
  422. dev_rescan_store);
  423. static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
  424. const char *buf, size_t count)
  425. {
  426. unsigned long val;
  427. if (kstrtoul(buf, 0, &val) < 0)
  428. return -EINVAL;
  429. if (val && device_remove_file_self(dev, attr))
  430. pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
  431. return count;
  432. }
  433. static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL,
  434. remove_store);
  435. static ssize_t bus_rescan_store(struct device *dev,
  436. struct device_attribute *attr,
  437. const char *buf, size_t count)
  438. {
  439. unsigned long val;
  440. struct pci_bus *bus = to_pci_bus(dev);
  441. if (kstrtoul(buf, 0, &val) < 0)
  442. return -EINVAL;
  443. if (val) {
  444. pci_lock_rescan_remove();
  445. if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
  446. pci_rescan_bus_bridge_resize(bus->self);
  447. else
  448. pci_rescan_bus(bus);
  449. pci_unlock_rescan_remove();
  450. }
  451. return count;
  452. }
  453. static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL,
  454. bus_rescan_store);
  455. static ssize_t reset_subordinate_store(struct device *dev,
  456. struct device_attribute *attr,
  457. const char *buf, size_t count)
  458. {
  459. struct pci_dev *pdev = to_pci_dev(dev);
  460. struct pci_bus *bus = pdev->subordinate;
  461. unsigned long val;
  462. if (!capable(CAP_SYS_ADMIN))
  463. return -EPERM;
  464. if (kstrtoul(buf, 0, &val) < 0)
  465. return -EINVAL;
  466. if (val) {
  467. int ret = __pci_reset_bus(bus);
  468. if (ret)
  469. return ret;
  470. }
  471. return count;
  472. }
  473. static DEVICE_ATTR_WO(reset_subordinate);
  474. #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
  475. static ssize_t d3cold_allowed_store(struct device *dev,
  476. struct device_attribute *attr,
  477. const char *buf, size_t count)
  478. {
  479. struct pci_dev *pdev = to_pci_dev(dev);
  480. unsigned long val;
  481. if (kstrtoul(buf, 0, &val) < 0)
  482. return -EINVAL;
  483. pdev->d3cold_allowed = !!val;
  484. pci_bridge_d3_update(pdev);
  485. pm_runtime_resume(dev);
  486. return count;
  487. }
  488. static ssize_t d3cold_allowed_show(struct device *dev,
  489. struct device_attribute *attr, char *buf)
  490. {
  491. struct pci_dev *pdev = to_pci_dev(dev);
  492. return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed);
  493. }
  494. static DEVICE_ATTR_RW(d3cold_allowed);
  495. #endif
  496. #ifdef CONFIG_OF
  497. static ssize_t devspec_show(struct device *dev,
  498. struct device_attribute *attr, char *buf)
  499. {
  500. struct pci_dev *pdev = to_pci_dev(dev);
  501. struct device_node *np = pci_device_to_OF_node(pdev);
  502. if (np == NULL)
  503. return 0;
  504. return sysfs_emit(buf, "%pOF\n", np);
  505. }
  506. static DEVICE_ATTR_RO(devspec);
  507. #endif
  508. static ssize_t driver_override_store(struct device *dev,
  509. struct device_attribute *attr,
  510. const char *buf, size_t count)
  511. {
  512. struct pci_dev *pdev = to_pci_dev(dev);
  513. int ret;
  514. ret = driver_set_override(dev, &pdev->driver_override, buf, count);
  515. if (ret)
  516. return ret;
  517. return count;
  518. }
  519. static ssize_t driver_override_show(struct device *dev,
  520. struct device_attribute *attr, char *buf)
  521. {
  522. struct pci_dev *pdev = to_pci_dev(dev);
  523. ssize_t len;
  524. device_lock(dev);
  525. len = sysfs_emit(buf, "%s\n", pdev->driver_override);
  526. device_unlock(dev);
  527. return len;
  528. }
  529. static DEVICE_ATTR_RW(driver_override);
  530. static struct attribute *pci_dev_attrs[] = {
  531. &dev_attr_power_state.attr,
  532. &dev_attr_resource.attr,
  533. &dev_attr_vendor.attr,
  534. &dev_attr_device.attr,
  535. &dev_attr_subsystem_vendor.attr,
  536. &dev_attr_subsystem_device.attr,
  537. &dev_attr_revision.attr,
  538. &dev_attr_class.attr,
  539. &dev_attr_irq.attr,
  540. &dev_attr_local_cpus.attr,
  541. &dev_attr_local_cpulist.attr,
  542. &dev_attr_modalias.attr,
  543. #ifdef CONFIG_NUMA
  544. &dev_attr_numa_node.attr,
  545. #endif
  546. &dev_attr_dma_mask_bits.attr,
  547. &dev_attr_consistent_dma_mask_bits.attr,
  548. &dev_attr_enable.attr,
  549. &dev_attr_broken_parity_status.attr,
  550. &dev_attr_msi_bus.attr,
  551. #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
  552. &dev_attr_d3cold_allowed.attr,
  553. #endif
  554. #ifdef CONFIG_OF
  555. &dev_attr_devspec.attr,
  556. #endif
  557. &dev_attr_driver_override.attr,
  558. &dev_attr_ari_enabled.attr,
  559. NULL,
  560. };
  561. static struct attribute *pci_bridge_attrs[] = {
  562. &dev_attr_subordinate_bus_number.attr,
  563. &dev_attr_secondary_bus_number.attr,
  564. &dev_attr_reset_subordinate.attr,
  565. NULL,
  566. };
  567. static struct attribute *pcie_dev_attrs[] = {
  568. &dev_attr_current_link_speed.attr,
  569. &dev_attr_current_link_width.attr,
  570. &dev_attr_max_link_width.attr,
  571. &dev_attr_max_link_speed.attr,
  572. NULL,
  573. };
  574. static struct attribute *pcibus_attrs[] = {
  575. &dev_attr_bus_rescan.attr,
  576. &dev_attr_cpuaffinity.attr,
  577. &dev_attr_cpulistaffinity.attr,
  578. NULL,
  579. };
  580. static const struct attribute_group pcibus_group = {
  581. .attrs = pcibus_attrs,
  582. };
  583. const struct attribute_group *pcibus_groups[] = {
  584. &pcibus_group,
  585. NULL,
  586. };
  587. static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
  588. char *buf)
  589. {
  590. struct pci_dev *pdev = to_pci_dev(dev);
  591. struct pci_dev *vga_dev = vga_default_device();
  592. if (vga_dev)
  593. return sysfs_emit(buf, "%u\n", (pdev == vga_dev));
  594. return sysfs_emit(buf, "%u\n",
  595. !!(pdev->resource[PCI_ROM_RESOURCE].flags &
  596. IORESOURCE_ROM_SHADOW));
  597. }
  598. static DEVICE_ATTR_RO(boot_vga);
  599. static ssize_t serial_number_show(struct device *dev,
  600. struct device_attribute *attr, char *buf)
  601. {
  602. struct pci_dev *pci_dev = to_pci_dev(dev);
  603. u64 dsn;
  604. u8 bytes[8];
  605. dsn = pci_get_dsn(pci_dev);
  606. if (!dsn)
  607. return -EIO;
  608. put_unaligned_be64(dsn, bytes);
  609. return sysfs_emit(buf, "%8phD\n", bytes);
  610. }
  611. static DEVICE_ATTR_ADMIN_RO(serial_number);
  612. static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
  613. const struct bin_attribute *bin_attr, char *buf,
  614. loff_t off, size_t count)
  615. {
  616. struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
  617. unsigned int size = 64;
  618. loff_t init_off = off;
  619. u8 *data = (u8 *) buf;
  620. /* Several chips lock up trying to read undefined config space */
  621. if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN))
  622. size = dev->cfg_size;
  623. else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  624. size = 128;
  625. if (off > size)
  626. return 0;
  627. if (off + count > size) {
  628. size -= off;
  629. count = size;
  630. } else {
  631. size = count;
  632. }
  633. pci_config_pm_runtime_get(dev);
  634. if ((off & 1) && size) {
  635. u8 val;
  636. pci_user_read_config_byte(dev, off, &val);
  637. data[off - init_off] = val;
  638. off++;
  639. size--;
  640. }
  641. if ((off & 3) && size > 2) {
  642. u16 val;
  643. pci_user_read_config_word(dev, off, &val);
  644. data[off - init_off] = val & 0xff;
  645. data[off - init_off + 1] = (val >> 8) & 0xff;
  646. off += 2;
  647. size -= 2;
  648. }
  649. while (size > 3) {
  650. u32 val;
  651. pci_user_read_config_dword(dev, off, &val);
  652. data[off - init_off] = val & 0xff;
  653. data[off - init_off + 1] = (val >> 8) & 0xff;
  654. data[off - init_off + 2] = (val >> 16) & 0xff;
  655. data[off - init_off + 3] = (val >> 24) & 0xff;
  656. off += 4;
  657. size -= 4;
  658. cond_resched();
  659. }
  660. if (size >= 2) {
  661. u16 val;
  662. pci_user_read_config_word(dev, off, &val);
  663. data[off - init_off] = val & 0xff;
  664. data[off - init_off + 1] = (val >> 8) & 0xff;
  665. off += 2;
  666. size -= 2;
  667. }
  668. if (size > 0) {
  669. u8 val;
  670. pci_user_read_config_byte(dev, off, &val);
  671. data[off - init_off] = val;
  672. }
  673. pci_config_pm_runtime_put(dev);
  674. return count;
  675. }
  676. static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
  677. const struct bin_attribute *bin_attr, char *buf,
  678. loff_t off, size_t count)
  679. {
  680. struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
  681. unsigned int size = count;
  682. loff_t init_off = off;
  683. u8 *data = (u8 *) buf;
  684. int ret;
  685. ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
  686. if (ret)
  687. return ret;
  688. if (resource_is_exclusive(&dev->driver_exclusive_resource, off,
  689. count)) {
  690. pci_warn_once(dev, "%s: Unexpected write to kernel-exclusive config offset %llx",
  691. current->comm, off);
  692. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  693. }
  694. if (off > dev->cfg_size)
  695. return 0;
  696. if (off + count > dev->cfg_size) {
  697. size = dev->cfg_size - off;
  698. count = size;
  699. }
  700. pci_config_pm_runtime_get(dev);
  701. if ((off & 1) && size) {
  702. pci_user_write_config_byte(dev, off, data[off - init_off]);
  703. off++;
  704. size--;
  705. }
  706. if ((off & 3) && size > 2) {
  707. u16 val = data[off - init_off];
  708. val |= (u16) data[off - init_off + 1] << 8;
  709. pci_user_write_config_word(dev, off, val);
  710. off += 2;
  711. size -= 2;
  712. }
  713. while (size > 3) {
  714. u32 val = data[off - init_off];
  715. val |= (u32) data[off - init_off + 1] << 8;
  716. val |= (u32) data[off - init_off + 2] << 16;
  717. val |= (u32) data[off - init_off + 3] << 24;
  718. pci_user_write_config_dword(dev, off, val);
  719. off += 4;
  720. size -= 4;
  721. }
  722. if (size >= 2) {
  723. u16 val = data[off - init_off];
  724. val |= (u16) data[off - init_off + 1] << 8;
  725. pci_user_write_config_word(dev, off, val);
  726. off += 2;
  727. size -= 2;
  728. }
  729. if (size)
  730. pci_user_write_config_byte(dev, off, data[off - init_off]);
  731. pci_config_pm_runtime_put(dev);
  732. return count;
  733. }
  734. static const BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0);
  735. static const struct bin_attribute *const pci_dev_config_attrs[] = {
  736. &bin_attr_config,
  737. NULL,
  738. };
  739. static size_t pci_dev_config_attr_bin_size(struct kobject *kobj,
  740. const struct bin_attribute *a,
  741. int n)
  742. {
  743. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  744. if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
  745. return PCI_CFG_SPACE_EXP_SIZE;
  746. return PCI_CFG_SPACE_SIZE;
  747. }
  748. static const struct attribute_group pci_dev_config_attr_group = {
  749. .bin_attrs = pci_dev_config_attrs,
  750. .bin_size = pci_dev_config_attr_bin_size,
  751. };
  752. /*
  753. * llseek operation for mmappable PCI resources.
  754. * May be left unused if the arch doesn't provide them.
  755. */
  756. static __maybe_unused loff_t
  757. pci_llseek_resource(struct file *filep,
  758. struct kobject *kobj __always_unused,
  759. const struct bin_attribute *attr,
  760. loff_t offset, int whence)
  761. {
  762. return fixed_size_llseek(filep, offset, whence, attr->size);
  763. }
  764. #ifdef HAVE_PCI_LEGACY
  765. /**
  766. * pci_read_legacy_io - read byte(s) from legacy I/O port space
  767. * @filp: open sysfs file
  768. * @kobj: kobject corresponding to file to read from
  769. * @bin_attr: struct bin_attribute for this file
  770. * @buf: buffer to store results
  771. * @off: offset into legacy I/O port space
  772. * @count: number of bytes to read
  773. *
  774. * Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  775. * callback routine (pci_legacy_read).
  776. */
  777. static ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj,
  778. const struct bin_attribute *bin_attr,
  779. char *buf, loff_t off, size_t count)
  780. {
  781. struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
  782. /* Only support 1, 2 or 4 byte accesses */
  783. if (count != 1 && count != 2 && count != 4)
  784. return -EINVAL;
  785. return pci_legacy_read(bus, off, (u32 *)buf, count);
  786. }
  787. /**
  788. * pci_write_legacy_io - write byte(s) to legacy I/O port space
  789. * @filp: open sysfs file
  790. * @kobj: kobject corresponding to file to read from
  791. * @bin_attr: struct bin_attribute for this file
  792. * @buf: buffer containing value to be written
  793. * @off: offset into legacy I/O port space
  794. * @count: number of bytes to write
  795. *
  796. * Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
  797. * callback routine (pci_legacy_write).
  798. */
  799. static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
  800. const struct bin_attribute *bin_attr,
  801. char *buf, loff_t off, size_t count)
  802. {
  803. struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
  804. /* Only support 1, 2 or 4 byte accesses */
  805. if (count != 1 && count != 2 && count != 4)
  806. return -EINVAL;
  807. return pci_legacy_write(bus, off, *(u32 *)buf, count);
  808. }
  809. /**
  810. * pci_mmap_legacy_mem - map legacy PCI memory into user memory space
  811. * @filp: open sysfs file
  812. * @kobj: kobject corresponding to device to be mapped
  813. * @attr: struct bin_attribute for this file
  814. * @vma: struct vm_area_struct passed to mmap
  815. *
  816. * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
  817. * legacy memory space (first meg of bus space) into application virtual
  818. * memory space.
  819. */
  820. static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
  821. const struct bin_attribute *attr,
  822. struct vm_area_struct *vma)
  823. {
  824. struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
  825. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
  826. }
  827. /**
  828. * pci_mmap_legacy_io - map legacy PCI IO into user memory space
  829. * @filp: open sysfs file
  830. * @kobj: kobject corresponding to device to be mapped
  831. * @attr: struct bin_attribute for this file
  832. * @vma: struct vm_area_struct passed to mmap
  833. *
  834. * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
  835. * legacy IO space (first meg of bus space) into application virtual
  836. * memory space. Returns -ENOSYS if the operation isn't supported
  837. */
  838. static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
  839. const struct bin_attribute *attr,
  840. struct vm_area_struct *vma)
  841. {
  842. struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
  843. return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
  844. }
  845. /**
  846. * pci_adjust_legacy_attr - adjustment of legacy file attributes
  847. * @b: bus to create files under
  848. * @mmap_type: I/O port or memory
  849. *
  850. * Stub implementation. Can be overridden by arch if necessary.
  851. */
  852. void __weak pci_adjust_legacy_attr(struct pci_bus *b,
  853. enum pci_mmap_state mmap_type)
  854. {
  855. }
  856. /**
  857. * pci_create_legacy_files - create legacy I/O port and memory files
  858. * @b: bus to create files under
  859. *
  860. * Some platforms allow access to legacy I/O port and ISA memory space on
  861. * a per-bus basis. This routine creates the files and ties them into
  862. * their associated read, write and mmap files from pci-sysfs.c
  863. *
  864. * On error unwind, but don't propagate the error to the caller
  865. * as it is ok to set up the PCI bus without these files.
  866. */
  867. void pci_create_legacy_files(struct pci_bus *b)
  868. {
  869. int error;
  870. if (!sysfs_initialized)
  871. return;
  872. b->legacy_io = kzalloc_objs(struct bin_attribute, 2, GFP_ATOMIC);
  873. if (!b->legacy_io)
  874. goto kzalloc_err;
  875. sysfs_bin_attr_init(b->legacy_io);
  876. b->legacy_io->attr.name = "legacy_io";
  877. b->legacy_io->size = 0xffff;
  878. b->legacy_io->attr.mode = 0600;
  879. b->legacy_io->read = pci_read_legacy_io;
  880. b->legacy_io->write = pci_write_legacy_io;
  881. /* See pci_create_attr() for motivation */
  882. b->legacy_io->llseek = pci_llseek_resource;
  883. b->legacy_io->mmap = pci_mmap_legacy_io;
  884. b->legacy_io->f_mapping = iomem_get_mapping;
  885. pci_adjust_legacy_attr(b, pci_mmap_io);
  886. error = device_create_bin_file(&b->dev, b->legacy_io);
  887. if (error)
  888. goto legacy_io_err;
  889. /* Allocated above after the legacy_io struct */
  890. b->legacy_mem = b->legacy_io + 1;
  891. sysfs_bin_attr_init(b->legacy_mem);
  892. b->legacy_mem->attr.name = "legacy_mem";
  893. b->legacy_mem->size = 1024*1024;
  894. b->legacy_mem->attr.mode = 0600;
  895. b->legacy_mem->mmap = pci_mmap_legacy_mem;
  896. /* See pci_create_attr() for motivation */
  897. b->legacy_mem->llseek = pci_llseek_resource;
  898. b->legacy_mem->f_mapping = iomem_get_mapping;
  899. pci_adjust_legacy_attr(b, pci_mmap_mem);
  900. error = device_create_bin_file(&b->dev, b->legacy_mem);
  901. if (error)
  902. goto legacy_mem_err;
  903. return;
  904. legacy_mem_err:
  905. device_remove_bin_file(&b->dev, b->legacy_io);
  906. legacy_io_err:
  907. kfree(b->legacy_io);
  908. b->legacy_io = NULL;
  909. kzalloc_err:
  910. dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n");
  911. }
  912. void pci_remove_legacy_files(struct pci_bus *b)
  913. {
  914. if (b->legacy_io) {
  915. device_remove_bin_file(&b->dev, b->legacy_io);
  916. device_remove_bin_file(&b->dev, b->legacy_mem);
  917. kfree(b->legacy_io); /* both are allocated here */
  918. }
  919. }
  920. #endif /* HAVE_PCI_LEGACY */
  921. #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
  922. /**
  923. * pci_mmap_resource - map a PCI resource into user memory space
  924. * @kobj: kobject for mapping
  925. * @attr: struct bin_attribute for the file being mapped
  926. * @vma: struct vm_area_struct passed into the mmap
  927. * @write_combine: 1 for write_combine mapping
  928. *
  929. * Use the regular PCI mapping routines to map a PCI resource into userspace.
  930. */
  931. static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *attr,
  932. struct vm_area_struct *vma, int write_combine)
  933. {
  934. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  935. int bar = (unsigned long)attr->private;
  936. enum pci_mmap_state mmap_type;
  937. struct resource *res = &pdev->resource[bar];
  938. int ret;
  939. ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
  940. if (ret)
  941. return ret;
  942. if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
  943. return -EINVAL;
  944. if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
  945. return -EINVAL;
  946. mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
  947. return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
  948. }
  949. static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
  950. const struct bin_attribute *attr,
  951. struct vm_area_struct *vma)
  952. {
  953. return pci_mmap_resource(kobj, attr, vma, 0);
  954. }
  955. static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
  956. const struct bin_attribute *attr,
  957. struct vm_area_struct *vma)
  958. {
  959. return pci_mmap_resource(kobj, attr, vma, 1);
  960. }
  961. static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
  962. const struct bin_attribute *attr, char *buf,
  963. loff_t off, size_t count, bool write)
  964. {
  965. #ifdef CONFIG_HAS_IOPORT
  966. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  967. int bar = (unsigned long)attr->private;
  968. unsigned long port = off;
  969. port += pci_resource_start(pdev, bar);
  970. if (port > pci_resource_end(pdev, bar))
  971. return 0;
  972. if (port + count - 1 > pci_resource_end(pdev, bar))
  973. return -EINVAL;
  974. switch (count) {
  975. case 1:
  976. if (write)
  977. outb(*(u8 *)buf, port);
  978. else
  979. *(u8 *)buf = inb(port);
  980. return 1;
  981. case 2:
  982. if (write)
  983. outw(*(u16 *)buf, port);
  984. else
  985. *(u16 *)buf = inw(port);
  986. return 2;
  987. case 4:
  988. if (write)
  989. outl(*(u32 *)buf, port);
  990. else
  991. *(u32 *)buf = inl(port);
  992. return 4;
  993. }
  994. return -EINVAL;
  995. #else
  996. return -ENXIO;
  997. #endif
  998. }
  999. static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
  1000. const struct bin_attribute *attr, char *buf,
  1001. loff_t off, size_t count)
  1002. {
  1003. return pci_resource_io(filp, kobj, attr, buf, off, count, false);
  1004. }
  1005. static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
  1006. const struct bin_attribute *attr, char *buf,
  1007. loff_t off, size_t count)
  1008. {
  1009. int ret;
  1010. ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
  1011. if (ret)
  1012. return ret;
  1013. return pci_resource_io(filp, kobj, attr, buf, off, count, true);
  1014. }
  1015. /**
  1016. * pci_remove_resource_files - cleanup resource files
  1017. * @pdev: dev to cleanup
  1018. *
  1019. * If we created resource files for @pdev, remove them from sysfs and
  1020. * free their resources.
  1021. */
  1022. static void pci_remove_resource_files(struct pci_dev *pdev)
  1023. {
  1024. int i;
  1025. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  1026. struct bin_attribute *res_attr;
  1027. res_attr = pdev->res_attr[i];
  1028. if (res_attr) {
  1029. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  1030. kfree(res_attr);
  1031. }
  1032. res_attr = pdev->res_attr_wc[i];
  1033. if (res_attr) {
  1034. sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
  1035. kfree(res_attr);
  1036. }
  1037. }
  1038. }
  1039. static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
  1040. {
  1041. /* allocate attribute structure, piggyback attribute name */
  1042. int name_len = write_combine ? 13 : 10;
  1043. struct bin_attribute *res_attr;
  1044. char *res_attr_name;
  1045. int retval;
  1046. res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
  1047. if (!res_attr)
  1048. return -ENOMEM;
  1049. res_attr_name = (char *)(res_attr + 1);
  1050. sysfs_bin_attr_init(res_attr);
  1051. if (write_combine) {
  1052. sprintf(res_attr_name, "resource%d_wc", num);
  1053. res_attr->mmap = pci_mmap_resource_wc;
  1054. } else {
  1055. sprintf(res_attr_name, "resource%d", num);
  1056. if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
  1057. res_attr->read = pci_read_resource_io;
  1058. res_attr->write = pci_write_resource_io;
  1059. if (arch_can_pci_mmap_io())
  1060. res_attr->mmap = pci_mmap_resource_uc;
  1061. } else {
  1062. res_attr->mmap = pci_mmap_resource_uc;
  1063. }
  1064. }
  1065. if (res_attr->mmap) {
  1066. res_attr->f_mapping = iomem_get_mapping;
  1067. /*
  1068. * generic_file_llseek() consults f_mapping->host to determine
  1069. * the file size. As iomem_inode knows nothing about the
  1070. * attribute, it's not going to work, so override it as well.
  1071. */
  1072. res_attr->llseek = pci_llseek_resource;
  1073. }
  1074. res_attr->attr.name = res_attr_name;
  1075. res_attr->attr.mode = 0600;
  1076. res_attr->size = pci_resource_len(pdev, num);
  1077. res_attr->private = (void *)(unsigned long)num;
  1078. retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
  1079. if (retval) {
  1080. kfree(res_attr);
  1081. return retval;
  1082. }
  1083. if (write_combine)
  1084. pdev->res_attr_wc[num] = res_attr;
  1085. else
  1086. pdev->res_attr[num] = res_attr;
  1087. return 0;
  1088. }
  1089. /**
  1090. * pci_create_resource_files - create resource files in sysfs for @dev
  1091. * @pdev: dev in question
  1092. *
  1093. * Walk the resources in @pdev creating files for each resource available.
  1094. */
  1095. static int pci_create_resource_files(struct pci_dev *pdev)
  1096. {
  1097. int i;
  1098. int retval;
  1099. /* Skip devices with non-mappable BARs */
  1100. if (pdev->non_mappable_bars)
  1101. return 0;
  1102. /* Expose the PCI resources from this device as files */
  1103. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  1104. /* skip empty resources */
  1105. if (!pci_resource_len(pdev, i))
  1106. continue;
  1107. retval = pci_create_attr(pdev, i, 0);
  1108. /* for prefetchable resources, create a WC mappable file */
  1109. if (!retval && arch_can_pci_mmap_wc() &&
  1110. pdev->resource[i].flags & IORESOURCE_PREFETCH)
  1111. retval = pci_create_attr(pdev, i, 1);
  1112. if (retval) {
  1113. pci_remove_resource_files(pdev);
  1114. return retval;
  1115. }
  1116. }
  1117. return 0;
  1118. }
  1119. #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */
  1120. int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
  1121. void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
  1122. #endif
  1123. /**
  1124. * pci_write_rom - used to enable access to the PCI ROM display
  1125. * @filp: sysfs file
  1126. * @kobj: kernel object handle
  1127. * @bin_attr: struct bin_attribute for this file
  1128. * @buf: user input
  1129. * @off: file offset
  1130. * @count: number of byte in input
  1131. *
  1132. * writing anything except 0 enables it
  1133. */
  1134. static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
  1135. const struct bin_attribute *bin_attr, char *buf,
  1136. loff_t off, size_t count)
  1137. {
  1138. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  1139. if ((off == 0) && (*buf == '0') && (count == 2))
  1140. pdev->rom_attr_enabled = 0;
  1141. else
  1142. pdev->rom_attr_enabled = 1;
  1143. return count;
  1144. }
  1145. /**
  1146. * pci_read_rom - read a PCI ROM
  1147. * @filp: sysfs file
  1148. * @kobj: kernel object handle
  1149. * @bin_attr: struct bin_attribute for this file
  1150. * @buf: where to put the data we read from the ROM
  1151. * @off: file offset
  1152. * @count: number of bytes to read
  1153. *
  1154. * Put @count bytes starting at @off into @buf from the ROM in the PCI
  1155. * device corresponding to @kobj.
  1156. */
  1157. static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj,
  1158. const struct bin_attribute *bin_attr, char *buf,
  1159. loff_t off, size_t count)
  1160. {
  1161. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  1162. void __iomem *rom;
  1163. size_t size;
  1164. if (!pdev->rom_attr_enabled)
  1165. return -EINVAL;
  1166. rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */
  1167. if (!rom || !size)
  1168. return -EIO;
  1169. if (off >= size)
  1170. count = 0;
  1171. else {
  1172. if (off + count > size)
  1173. count = size - off;
  1174. memcpy_fromio(buf, rom + off, count);
  1175. }
  1176. pci_unmap_rom(pdev, rom);
  1177. return count;
  1178. }
  1179. static const BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0);
  1180. static const struct bin_attribute *const pci_dev_rom_attrs[] = {
  1181. &bin_attr_rom,
  1182. NULL,
  1183. };
  1184. static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
  1185. const struct bin_attribute *a, int n)
  1186. {
  1187. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  1188. /* If the device has a ROM, try to expose it in sysfs. */
  1189. if (!pci_resource_end(pdev, PCI_ROM_RESOURCE))
  1190. return 0;
  1191. return a->attr.mode;
  1192. }
  1193. static size_t pci_dev_rom_attr_bin_size(struct kobject *kobj,
  1194. const struct bin_attribute *a, int n)
  1195. {
  1196. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  1197. return pci_resource_len(pdev, PCI_ROM_RESOURCE);
  1198. }
  1199. static const struct attribute_group pci_dev_rom_attr_group = {
  1200. .bin_attrs = pci_dev_rom_attrs,
  1201. .is_bin_visible = pci_dev_rom_attr_is_visible,
  1202. .bin_size = pci_dev_rom_attr_bin_size,
  1203. };
  1204. static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
  1205. const char *buf, size_t count)
  1206. {
  1207. struct pci_dev *pdev = to_pci_dev(dev);
  1208. unsigned long val;
  1209. ssize_t result;
  1210. if (kstrtoul(buf, 0, &val) < 0)
  1211. return -EINVAL;
  1212. if (val != 1)
  1213. return -EINVAL;
  1214. pm_runtime_get_sync(dev);
  1215. result = pci_reset_function(pdev);
  1216. pm_runtime_put(dev);
  1217. if (result < 0)
  1218. return result;
  1219. return count;
  1220. }
  1221. static DEVICE_ATTR_WO(reset);
  1222. static struct attribute *pci_dev_reset_attrs[] = {
  1223. &dev_attr_reset.attr,
  1224. NULL,
  1225. };
  1226. static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj,
  1227. struct attribute *a, int n)
  1228. {
  1229. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  1230. if (!pci_reset_supported(pdev))
  1231. return 0;
  1232. return a->mode;
  1233. }
  1234. static const struct attribute_group pci_dev_reset_attr_group = {
  1235. .attrs = pci_dev_reset_attrs,
  1236. .is_visible = pci_dev_reset_attr_is_visible,
  1237. };
  1238. static ssize_t reset_method_show(struct device *dev,
  1239. struct device_attribute *attr, char *buf)
  1240. {
  1241. struct pci_dev *pdev = to_pci_dev(dev);
  1242. ssize_t len = 0;
  1243. int i, m;
  1244. for (i = 0; i < PCI_NUM_RESET_METHODS; i++) {
  1245. m = pdev->reset_methods[i];
  1246. if (!m)
  1247. break;
  1248. len += sysfs_emit_at(buf, len, "%s%s", len ? " " : "",
  1249. pci_reset_fn_methods[m].name);
  1250. }
  1251. if (len)
  1252. len += sysfs_emit_at(buf, len, "\n");
  1253. return len;
  1254. }
  1255. static int reset_method_lookup(const char *name)
  1256. {
  1257. int m;
  1258. for (m = 1; m < PCI_NUM_RESET_METHODS; m++) {
  1259. if (sysfs_streq(name, pci_reset_fn_methods[m].name))
  1260. return m;
  1261. }
  1262. return 0; /* not found */
  1263. }
  1264. static ssize_t reset_method_store(struct device *dev,
  1265. struct device_attribute *attr,
  1266. const char *buf, size_t count)
  1267. {
  1268. struct pci_dev *pdev = to_pci_dev(dev);
  1269. char *tmp_options, *name;
  1270. int m, n;
  1271. u8 reset_methods[PCI_NUM_RESET_METHODS] = {};
  1272. if (sysfs_streq(buf, "")) {
  1273. pdev->reset_methods[0] = 0;
  1274. pci_warn(pdev, "All device reset methods disabled by user");
  1275. return count;
  1276. }
  1277. PM_RUNTIME_ACQUIRE(dev, pm);
  1278. if (PM_RUNTIME_ACQUIRE_ERR(&pm))
  1279. return -ENXIO;
  1280. if (sysfs_streq(buf, "default")) {
  1281. pci_init_reset_methods(pdev);
  1282. return count;
  1283. }
  1284. char *options __free(kfree) = kstrndup(buf, count, GFP_KERNEL);
  1285. if (!options)
  1286. return -ENOMEM;
  1287. n = 0;
  1288. tmp_options = options;
  1289. while ((name = strsep(&tmp_options, " ")) != NULL) {
  1290. if (sysfs_streq(name, ""))
  1291. continue;
  1292. name = strim(name);
  1293. /* Leave previous methods unchanged if input is invalid */
  1294. m = reset_method_lookup(name);
  1295. if (!m) {
  1296. pci_err(pdev, "Invalid reset method '%s'", name);
  1297. return -EINVAL;
  1298. }
  1299. if (pci_reset_fn_methods[m].reset_fn(pdev, PCI_RESET_PROBE)) {
  1300. pci_err(pdev, "Unsupported reset method '%s'", name);
  1301. return -EINVAL;
  1302. }
  1303. if (n == PCI_NUM_RESET_METHODS - 1) {
  1304. pci_err(pdev, "Too many reset methods\n");
  1305. return -EINVAL;
  1306. }
  1307. reset_methods[n++] = m;
  1308. }
  1309. reset_methods[n] = 0;
  1310. /* Warn if dev-specific supported but not highest priority */
  1311. if (pci_reset_fn_methods[1].reset_fn(pdev, PCI_RESET_PROBE) == 0 &&
  1312. reset_methods[0] != 1)
  1313. pci_warn(pdev, "Device-specific reset disabled/de-prioritized by user");
  1314. memcpy(pdev->reset_methods, reset_methods, sizeof(pdev->reset_methods));
  1315. return count;
  1316. }
  1317. static DEVICE_ATTR_RW(reset_method);
  1318. static struct attribute *pci_dev_reset_method_attrs[] = {
  1319. &dev_attr_reset_method.attr,
  1320. NULL,
  1321. };
  1322. static const struct attribute_group pci_dev_reset_method_attr_group = {
  1323. .attrs = pci_dev_reset_method_attrs,
  1324. .is_visible = pci_dev_reset_attr_is_visible,
  1325. };
  1326. static ssize_t __resource_resize_show(struct device *dev, int n, char *buf)
  1327. {
  1328. struct pci_dev *pdev = to_pci_dev(dev);
  1329. ssize_t ret;
  1330. pci_config_pm_runtime_get(pdev);
  1331. ret = sysfs_emit(buf, "%016llx\n",
  1332. pci_rebar_get_possible_sizes(pdev, n));
  1333. pci_config_pm_runtime_put(pdev);
  1334. return ret;
  1335. }
  1336. static ssize_t __resource_resize_store(struct device *dev, int n,
  1337. const char *buf, size_t count)
  1338. {
  1339. struct pci_dev *pdev = to_pci_dev(dev);
  1340. struct pci_bus *bus = pdev->bus;
  1341. unsigned long size;
  1342. int ret;
  1343. u16 cmd;
  1344. if (kstrtoul(buf, 0, &size) < 0)
  1345. return -EINVAL;
  1346. device_lock(dev);
  1347. if (dev->driver || pci_num_vf(pdev)) {
  1348. ret = -EBUSY;
  1349. goto unlock;
  1350. }
  1351. pci_config_pm_runtime_get(pdev);
  1352. if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
  1353. ret = aperture_remove_conflicting_pci_devices(pdev,
  1354. "resourceN_resize");
  1355. if (ret)
  1356. goto pm_put;
  1357. }
  1358. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  1359. pci_write_config_word(pdev, PCI_COMMAND,
  1360. cmd & ~PCI_COMMAND_MEMORY);
  1361. pci_remove_resource_files(pdev);
  1362. ret = pci_resize_resource(pdev, n, size, 0);
  1363. pci_assign_unassigned_bus_resources(bus);
  1364. if (pci_create_resource_files(pdev))
  1365. pci_warn(pdev, "Failed to recreate resource files after BAR resizing\n");
  1366. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  1367. pm_put:
  1368. pci_config_pm_runtime_put(pdev);
  1369. unlock:
  1370. device_unlock(dev);
  1371. return ret ? ret : count;
  1372. }
  1373. #define pci_dev_resource_resize_attr(n) \
  1374. static ssize_t resource##n##_resize_show(struct device *dev, \
  1375. struct device_attribute *attr, \
  1376. char *buf) \
  1377. { \
  1378. return __resource_resize_show(dev, n, buf); \
  1379. } \
  1380. static ssize_t resource##n##_resize_store(struct device *dev, \
  1381. struct device_attribute *attr,\
  1382. const char *buf, size_t count)\
  1383. { \
  1384. return __resource_resize_store(dev, n, buf, count); \
  1385. } \
  1386. static DEVICE_ATTR_RW(resource##n##_resize)
  1387. pci_dev_resource_resize_attr(0);
  1388. pci_dev_resource_resize_attr(1);
  1389. pci_dev_resource_resize_attr(2);
  1390. pci_dev_resource_resize_attr(3);
  1391. pci_dev_resource_resize_attr(4);
  1392. pci_dev_resource_resize_attr(5);
  1393. static struct attribute *resource_resize_attrs[] = {
  1394. &dev_attr_resource0_resize.attr,
  1395. &dev_attr_resource1_resize.attr,
  1396. &dev_attr_resource2_resize.attr,
  1397. &dev_attr_resource3_resize.attr,
  1398. &dev_attr_resource4_resize.attr,
  1399. &dev_attr_resource5_resize.attr,
  1400. NULL,
  1401. };
  1402. static umode_t resource_resize_is_visible(struct kobject *kobj,
  1403. struct attribute *a, int n)
  1404. {
  1405. struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
  1406. return pci_rebar_get_current_size(pdev, n) < 0 ? 0 : a->mode;
  1407. }
  1408. static const struct attribute_group pci_dev_resource_resize_group = {
  1409. .attrs = resource_resize_attrs,
  1410. .is_visible = resource_resize_is_visible,
  1411. };
  1412. int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
  1413. {
  1414. if (!sysfs_initialized)
  1415. return -EACCES;
  1416. return pci_create_resource_files(pdev);
  1417. }
  1418. /**
  1419. * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files
  1420. * @pdev: device whose entries we should free
  1421. *
  1422. * Cleanup when @pdev is removed from sysfs.
  1423. */
  1424. void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
  1425. {
  1426. if (!sysfs_initialized)
  1427. return;
  1428. pci_remove_resource_files(pdev);
  1429. }
  1430. static int __init pci_sysfs_init(void)
  1431. {
  1432. struct pci_dev *pdev = NULL;
  1433. struct pci_bus *pbus = NULL;
  1434. int retval;
  1435. sysfs_initialized = 1;
  1436. for_each_pci_dev(pdev) {
  1437. retval = pci_create_sysfs_dev_files(pdev);
  1438. if (retval) {
  1439. pci_dev_put(pdev);
  1440. return retval;
  1441. }
  1442. }
  1443. while ((pbus = pci_find_next_bus(pbus)))
  1444. pci_create_legacy_files(pbus);
  1445. return 0;
  1446. }
  1447. late_initcall(pci_sysfs_init);
  1448. static struct attribute *pci_dev_dev_attrs[] = {
  1449. &dev_attr_boot_vga.attr,
  1450. &dev_attr_serial_number.attr,
  1451. NULL,
  1452. };
  1453. static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
  1454. struct attribute *a, int n)
  1455. {
  1456. struct device *dev = kobj_to_dev(kobj);
  1457. struct pci_dev *pdev = to_pci_dev(dev);
  1458. if (a == &dev_attr_boot_vga.attr && pci_is_vga(pdev))
  1459. return a->mode;
  1460. if (a == &dev_attr_serial_number.attr && pci_get_dsn(pdev))
  1461. return a->mode;
  1462. return 0;
  1463. }
  1464. static struct attribute *pci_dev_hp_attrs[] = {
  1465. &dev_attr_remove.attr,
  1466. &dev_attr_dev_rescan.attr,
  1467. NULL,
  1468. };
  1469. static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj,
  1470. struct attribute *a, int n)
  1471. {
  1472. struct device *dev = kobj_to_dev(kobj);
  1473. struct pci_dev *pdev = to_pci_dev(dev);
  1474. if (pdev->is_virtfn)
  1475. return 0;
  1476. return a->mode;
  1477. }
  1478. static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj,
  1479. struct attribute *a, int n)
  1480. {
  1481. struct device *dev = kobj_to_dev(kobj);
  1482. struct pci_dev *pdev = to_pci_dev(dev);
  1483. if (pci_is_bridge(pdev))
  1484. return a->mode;
  1485. return 0;
  1486. }
  1487. static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj,
  1488. struct attribute *a, int n)
  1489. {
  1490. struct device *dev = kobj_to_dev(kobj);
  1491. struct pci_dev *pdev = to_pci_dev(dev);
  1492. if (pci_is_pcie(pdev))
  1493. return a->mode;
  1494. return 0;
  1495. }
  1496. static const struct attribute_group pci_dev_group = {
  1497. .attrs = pci_dev_attrs,
  1498. };
  1499. const struct attribute_group *pci_dev_groups[] = {
  1500. &pci_dev_group,
  1501. &pci_dev_config_attr_group,
  1502. &pci_dev_rom_attr_group,
  1503. &pci_dev_reset_attr_group,
  1504. &pci_dev_reset_method_attr_group,
  1505. &pci_dev_vpd_attr_group,
  1506. #ifdef CONFIG_DMI
  1507. &pci_dev_smbios_attr_group,
  1508. #endif
  1509. #ifdef CONFIG_ACPI
  1510. &pci_dev_acpi_attr_group,
  1511. #endif
  1512. &pci_dev_resource_resize_group,
  1513. ARCH_PCI_DEV_GROUPS
  1514. NULL,
  1515. };
  1516. static const struct attribute_group pci_dev_hp_attr_group = {
  1517. .attrs = pci_dev_hp_attrs,
  1518. .is_visible = pci_dev_hp_attrs_are_visible,
  1519. };
  1520. static const struct attribute_group pci_dev_attr_group = {
  1521. .attrs = pci_dev_dev_attrs,
  1522. .is_visible = pci_dev_attrs_are_visible,
  1523. };
  1524. static const struct attribute_group pci_bridge_attr_group = {
  1525. .attrs = pci_bridge_attrs,
  1526. .is_visible = pci_bridge_attrs_are_visible,
  1527. };
  1528. static const struct attribute_group pcie_dev_attr_group = {
  1529. .attrs = pcie_dev_attrs,
  1530. .is_visible = pcie_dev_attrs_are_visible,
  1531. };
  1532. const struct attribute_group *pci_dev_attr_groups[] = {
  1533. &pci_dev_attr_group,
  1534. &pci_dev_hp_attr_group,
  1535. #ifdef CONFIG_PCI_IOV
  1536. &sriov_pf_dev_attr_group,
  1537. &sriov_vf_dev_attr_group,
  1538. #endif
  1539. &pci_bridge_attr_group,
  1540. &pcie_dev_attr_group,
  1541. #ifdef CONFIG_PCIEAER
  1542. &aer_stats_attr_group,
  1543. &aer_attr_group,
  1544. #endif
  1545. #ifdef CONFIG_PCIEASPM
  1546. &aspm_ctrl_attr_group,
  1547. #endif
  1548. #ifdef CONFIG_PCI_DOE
  1549. &pci_doe_sysfs_group,
  1550. #endif
  1551. #ifdef CONFIG_PCI_TSM
  1552. &pci_tsm_auth_attr_group,
  1553. &pci_tsm_attr_group,
  1554. #endif
  1555. NULL,
  1556. };