conf_space_header.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * PCI Backend - Handles the virtual fields in the configuration space headers.
  3. *
  4. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/pci.h>
  9. #include "pciback.h"
  10. #include "conf_space.h"
  11. struct pci_cmd_info {
  12. u16 val;
  13. };
  14. struct pci_bar_info {
  15. u32 val;
  16. u32 len_val;
  17. int which;
  18. };
  19. #define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO))
  20. #define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER)
  21. /* Bits guests are allowed to control in permissive mode. */
  22. #define PCI_COMMAND_GUEST (PCI_COMMAND_MASTER|PCI_COMMAND_SPECIAL| \
  23. PCI_COMMAND_INVALIDATE|PCI_COMMAND_VGA_PALETTE| \
  24. PCI_COMMAND_WAIT|PCI_COMMAND_FAST_BACK)
  25. static void *command_init(struct pci_dev *dev, int offset)
  26. {
  27. struct pci_cmd_info *cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  28. int err;
  29. if (!cmd)
  30. return ERR_PTR(-ENOMEM);
  31. err = pci_read_config_word(dev, PCI_COMMAND, &cmd->val);
  32. if (err) {
  33. kfree(cmd);
  34. return ERR_PTR(err);
  35. }
  36. return cmd;
  37. }
  38. static int command_read(struct pci_dev *dev, int offset, u16 *value, void *data)
  39. {
  40. int ret = pci_read_config_word(dev, offset, value);
  41. const struct pci_cmd_info *cmd = data;
  42. *value &= PCI_COMMAND_GUEST;
  43. *value |= cmd->val & ~PCI_COMMAND_GUEST;
  44. return ret;
  45. }
  46. static int command_write(struct pci_dev *dev, int offset, u16 value, void *data)
  47. {
  48. struct xen_pcibk_dev_data *dev_data;
  49. int err;
  50. u16 val;
  51. struct pci_cmd_info *cmd = data;
  52. dev_data = pci_get_drvdata(dev);
  53. if (!pci_is_enabled(dev) && is_enable_cmd(value)) {
  54. if (unlikely(verbose_request))
  55. printk(KERN_DEBUG DRV_NAME ": %s: enable\n",
  56. pci_name(dev));
  57. err = pci_enable_device(dev);
  58. if (err)
  59. return err;
  60. if (dev_data)
  61. dev_data->enable_intx = 1;
  62. } else if (pci_is_enabled(dev) && !is_enable_cmd(value)) {
  63. if (unlikely(verbose_request))
  64. printk(KERN_DEBUG DRV_NAME ": %s: disable\n",
  65. pci_name(dev));
  66. pci_disable_device(dev);
  67. if (dev_data)
  68. dev_data->enable_intx = 0;
  69. }
  70. if (!dev->is_busmaster && is_master_cmd(value)) {
  71. if (unlikely(verbose_request))
  72. printk(KERN_DEBUG DRV_NAME ": %s: set bus master\n",
  73. pci_name(dev));
  74. pci_set_master(dev);
  75. } else if (dev->is_busmaster && !is_master_cmd(value)) {
  76. if (unlikely(verbose_request))
  77. printk(KERN_DEBUG DRV_NAME ": %s: clear bus master\n",
  78. pci_name(dev));
  79. pci_clear_master(dev);
  80. }
  81. if (!(cmd->val & PCI_COMMAND_INVALIDATE) &&
  82. (value & PCI_COMMAND_INVALIDATE)) {
  83. if (unlikely(verbose_request))
  84. printk(KERN_DEBUG
  85. DRV_NAME ": %s: enable memory-write-invalidate\n",
  86. pci_name(dev));
  87. err = pci_set_mwi(dev);
  88. if (err) {
  89. pr_warn("%s: cannot enable memory-write-invalidate (%d)\n",
  90. pci_name(dev), err);
  91. value &= ~PCI_COMMAND_INVALIDATE;
  92. }
  93. } else if ((cmd->val & PCI_COMMAND_INVALIDATE) &&
  94. !(value & PCI_COMMAND_INVALIDATE)) {
  95. if (unlikely(verbose_request))
  96. printk(KERN_DEBUG
  97. DRV_NAME ": %s: disable memory-write-invalidate\n",
  98. pci_name(dev));
  99. pci_clear_mwi(dev);
  100. }
  101. cmd->val = value;
  102. if (!xen_pcibk_permissive && (!dev_data || !dev_data->permissive))
  103. return 0;
  104. /* Only allow the guest to control certain bits. */
  105. err = pci_read_config_word(dev, offset, &val);
  106. if (err || val == value)
  107. return err;
  108. value &= PCI_COMMAND_GUEST;
  109. value |= val & ~PCI_COMMAND_GUEST;
  110. return pci_write_config_word(dev, offset, value);
  111. }
  112. static int rom_write(struct pci_dev *dev, int offset, u32 value, void *data)
  113. {
  114. struct pci_bar_info *bar = data;
  115. if (unlikely(!bar)) {
  116. pr_warn(DRV_NAME ": driver data not found for %s\n",
  117. pci_name(dev));
  118. return XEN_PCI_ERR_op_failed;
  119. }
  120. /* A write to obtain the length must happen as a 32-bit write.
  121. * This does not (yet) support writing individual bytes
  122. */
  123. if ((value | ~PCI_ROM_ADDRESS_MASK) == ~0U)
  124. bar->which = 1;
  125. else {
  126. u32 tmpval;
  127. pci_read_config_dword(dev, offset, &tmpval);
  128. if (tmpval != bar->val && value == bar->val) {
  129. /* Allow restoration of bar value. */
  130. pci_write_config_dword(dev, offset, bar->val);
  131. }
  132. bar->which = 0;
  133. }
  134. /* Do we need to support enabling/disabling the rom address here? */
  135. return 0;
  136. }
  137. /* For the BARs, only allow writes which write ~0 or
  138. * the correct resource information
  139. * (Needed for when the driver probes the resource usage)
  140. */
  141. static int bar_write(struct pci_dev *dev, int offset, u32 value, void *data)
  142. {
  143. struct pci_bar_info *bar = data;
  144. unsigned int pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  145. const struct resource *res = dev->resource;
  146. u32 mask;
  147. if (unlikely(!bar)) {
  148. pr_warn(DRV_NAME ": driver data not found for %s\n",
  149. pci_name(dev));
  150. return XEN_PCI_ERR_op_failed;
  151. }
  152. /* A write to obtain the length must happen as a 32-bit write.
  153. * This does not (yet) support writing individual bytes
  154. */
  155. if (res[pos].flags & IORESOURCE_IO)
  156. mask = ~PCI_BASE_ADDRESS_IO_MASK;
  157. else if (pos && (res[pos - 1].flags & IORESOURCE_MEM_64))
  158. mask = 0;
  159. else
  160. mask = ~PCI_BASE_ADDRESS_MEM_MASK;
  161. if ((value | mask) == ~0U)
  162. bar->which = 1;
  163. else {
  164. u32 tmpval;
  165. pci_read_config_dword(dev, offset, &tmpval);
  166. if (tmpval != bar->val && value == bar->val) {
  167. /* Allow restoration of bar value. */
  168. pci_write_config_dword(dev, offset, bar->val);
  169. }
  170. bar->which = 0;
  171. }
  172. return 0;
  173. }
  174. static int bar_read(struct pci_dev *dev, int offset, u32 * value, void *data)
  175. {
  176. struct pci_bar_info *bar = data;
  177. if (unlikely(!bar)) {
  178. pr_warn(DRV_NAME ": driver data not found for %s\n",
  179. pci_name(dev));
  180. return XEN_PCI_ERR_op_failed;
  181. }
  182. *value = bar->which ? bar->len_val : bar->val;
  183. return 0;
  184. }
  185. static void *bar_init(struct pci_dev *dev, int offset)
  186. {
  187. unsigned int pos;
  188. const struct resource *res = dev->resource;
  189. struct pci_bar_info *bar = kzalloc(sizeof(*bar), GFP_KERNEL);
  190. if (!bar)
  191. return ERR_PTR(-ENOMEM);
  192. if (offset == PCI_ROM_ADDRESS || offset == PCI_ROM_ADDRESS1)
  193. pos = PCI_ROM_RESOURCE;
  194. else {
  195. pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  196. if (pos && (res[pos - 1].flags & IORESOURCE_MEM_64)) {
  197. bar->val = res[pos - 1].start >> 32;
  198. bar->len_val = -resource_size(&res[pos - 1]) >> 32;
  199. return bar;
  200. }
  201. }
  202. if (!res[pos].flags ||
  203. (res[pos].flags & (IORESOURCE_DISABLED | IORESOURCE_UNSET |
  204. IORESOURCE_BUSY)))
  205. return bar;
  206. bar->val = res[pos].start |
  207. (res[pos].flags & PCI_REGION_FLAG_MASK);
  208. bar->len_val = -resource_size(&res[pos]) |
  209. (res[pos].flags & PCI_REGION_FLAG_MASK);
  210. return bar;
  211. }
  212. static void bar_reset(struct pci_dev *dev, int offset, void *data)
  213. {
  214. struct pci_bar_info *bar = data;
  215. bar->which = 0;
  216. }
  217. static void bar_release(struct pci_dev *dev, int offset, void *data)
  218. {
  219. kfree(data);
  220. }
  221. static int xen_pcibk_read_vendor(struct pci_dev *dev, int offset,
  222. u16 *value, void *data)
  223. {
  224. *value = dev->vendor;
  225. return 0;
  226. }
  227. static int xen_pcibk_read_device(struct pci_dev *dev, int offset,
  228. u16 *value, void *data)
  229. {
  230. *value = dev->device;
  231. return 0;
  232. }
  233. static int interrupt_read(struct pci_dev *dev, int offset, u8 * value,
  234. void *data)
  235. {
  236. *value = (u8) dev->irq;
  237. return 0;
  238. }
  239. static int bist_write(struct pci_dev *dev, int offset, u8 value, void *data)
  240. {
  241. u8 cur_value;
  242. int err;
  243. err = pci_read_config_byte(dev, offset, &cur_value);
  244. if (err)
  245. goto out;
  246. if ((cur_value & ~PCI_BIST_START) == (value & ~PCI_BIST_START)
  247. || value == PCI_BIST_START)
  248. err = pci_write_config_byte(dev, offset, value);
  249. out:
  250. return err;
  251. }
  252. static const struct config_field header_common[] = {
  253. {
  254. .offset = PCI_VENDOR_ID,
  255. .size = 2,
  256. .u.w.read = xen_pcibk_read_vendor,
  257. },
  258. {
  259. .offset = PCI_DEVICE_ID,
  260. .size = 2,
  261. .u.w.read = xen_pcibk_read_device,
  262. },
  263. {
  264. .offset = PCI_COMMAND,
  265. .size = 2,
  266. .init = command_init,
  267. .release = bar_release,
  268. .u.w.read = command_read,
  269. .u.w.write = command_write,
  270. },
  271. {
  272. .offset = PCI_INTERRUPT_LINE,
  273. .size = 1,
  274. .u.b.read = interrupt_read,
  275. },
  276. {
  277. .offset = PCI_INTERRUPT_PIN,
  278. .size = 1,
  279. .u.b.read = xen_pcibk_read_config_byte,
  280. },
  281. {
  282. /* Any side effects of letting driver domain control cache line? */
  283. .offset = PCI_CACHE_LINE_SIZE,
  284. .size = 1,
  285. .u.b.read = xen_pcibk_read_config_byte,
  286. .u.b.write = xen_pcibk_write_config_byte,
  287. },
  288. {
  289. .offset = PCI_LATENCY_TIMER,
  290. .size = 1,
  291. .u.b.read = xen_pcibk_read_config_byte,
  292. },
  293. {
  294. .offset = PCI_BIST,
  295. .size = 1,
  296. .u.b.read = xen_pcibk_read_config_byte,
  297. .u.b.write = bist_write,
  298. },
  299. {}
  300. };
  301. #define CFG_FIELD_BAR(reg_offset) \
  302. { \
  303. .offset = reg_offset, \
  304. .size = 4, \
  305. .init = bar_init, \
  306. .reset = bar_reset, \
  307. .release = bar_release, \
  308. .u.dw.read = bar_read, \
  309. .u.dw.write = bar_write, \
  310. }
  311. #define CFG_FIELD_ROM(reg_offset) \
  312. { \
  313. .offset = reg_offset, \
  314. .size = 4, \
  315. .init = bar_init, \
  316. .reset = bar_reset, \
  317. .release = bar_release, \
  318. .u.dw.read = bar_read, \
  319. .u.dw.write = rom_write, \
  320. }
  321. static const struct config_field header_0[] = {
  322. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  323. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  324. CFG_FIELD_BAR(PCI_BASE_ADDRESS_2),
  325. CFG_FIELD_BAR(PCI_BASE_ADDRESS_3),
  326. CFG_FIELD_BAR(PCI_BASE_ADDRESS_4),
  327. CFG_FIELD_BAR(PCI_BASE_ADDRESS_5),
  328. CFG_FIELD_ROM(PCI_ROM_ADDRESS),
  329. {}
  330. };
  331. static const struct config_field header_1[] = {
  332. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  333. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  334. CFG_FIELD_ROM(PCI_ROM_ADDRESS1),
  335. {}
  336. };
  337. int xen_pcibk_config_header_add_fields(struct pci_dev *dev)
  338. {
  339. int err;
  340. err = xen_pcibk_config_add_fields(dev, header_common);
  341. if (err)
  342. goto out;
  343. switch (dev->hdr_type) {
  344. case PCI_HEADER_TYPE_NORMAL:
  345. err = xen_pcibk_config_add_fields(dev, header_0);
  346. break;
  347. case PCI_HEADER_TYPE_BRIDGE:
  348. err = xen_pcibk_config_add_fields(dev, header_1);
  349. break;
  350. default:
  351. err = -EINVAL;
  352. pr_err("%s: Unsupported header type %d!\n",
  353. pci_name(dev), dev->hdr_type);
  354. break;
  355. }
  356. out:
  357. return err;
  358. }