conf_space_header.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_ENABLE)
  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. if (unlikely(!bar)) {
  145. pr_warn(DRV_NAME ": driver data not found for %s\n",
  146. pci_name(dev));
  147. return XEN_PCI_ERR_op_failed;
  148. }
  149. /* A write to obtain the length must happen as a 32-bit write.
  150. * This does not (yet) support writing individual bytes
  151. */
  152. if (value == ~0)
  153. bar->which = 1;
  154. else {
  155. u32 tmpval;
  156. pci_read_config_dword(dev, offset, &tmpval);
  157. if (tmpval != bar->val && value == bar->val) {
  158. /* Allow restoration of bar value. */
  159. pci_write_config_dword(dev, offset, bar->val);
  160. }
  161. bar->which = 0;
  162. }
  163. return 0;
  164. }
  165. static int bar_read(struct pci_dev *dev, int offset, u32 * value, void *data)
  166. {
  167. struct pci_bar_info *bar = data;
  168. if (unlikely(!bar)) {
  169. pr_warn(DRV_NAME ": driver data not found for %s\n",
  170. pci_name(dev));
  171. return XEN_PCI_ERR_op_failed;
  172. }
  173. *value = bar->which ? bar->len_val : bar->val;
  174. return 0;
  175. }
  176. static inline void read_dev_bar(struct pci_dev *dev,
  177. struct pci_bar_info *bar_info, int offset,
  178. u32 len_mask)
  179. {
  180. int pos;
  181. struct resource *res = dev->resource;
  182. if (offset == PCI_ROM_ADDRESS || offset == PCI_ROM_ADDRESS1)
  183. pos = PCI_ROM_RESOURCE;
  184. else {
  185. pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  186. if (pos && ((res[pos - 1].flags & (PCI_BASE_ADDRESS_SPACE |
  187. PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  188. (PCI_BASE_ADDRESS_SPACE_MEMORY |
  189. PCI_BASE_ADDRESS_MEM_TYPE_64))) {
  190. bar_info->val = res[pos - 1].start >> 32;
  191. bar_info->len_val = res[pos - 1].end >> 32;
  192. return;
  193. }
  194. }
  195. bar_info->val = res[pos].start |
  196. (res[pos].flags & PCI_REGION_FLAG_MASK);
  197. bar_info->len_val = resource_size(&res[pos]);
  198. }
  199. static void *bar_init(struct pci_dev *dev, int offset)
  200. {
  201. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  202. if (!bar)
  203. return ERR_PTR(-ENOMEM);
  204. read_dev_bar(dev, bar, offset, ~0);
  205. bar->which = 0;
  206. return bar;
  207. }
  208. static void *rom_init(struct pci_dev *dev, int offset)
  209. {
  210. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  211. if (!bar)
  212. return ERR_PTR(-ENOMEM);
  213. read_dev_bar(dev, bar, offset, ~PCI_ROM_ADDRESS_ENABLE);
  214. bar->which = 0;
  215. return bar;
  216. }
  217. static void bar_reset(struct pci_dev *dev, int offset, void *data)
  218. {
  219. struct pci_bar_info *bar = data;
  220. bar->which = 0;
  221. }
  222. static void bar_release(struct pci_dev *dev, int offset, void *data)
  223. {
  224. kfree(data);
  225. }
  226. static int xen_pcibk_read_vendor(struct pci_dev *dev, int offset,
  227. u16 *value, void *data)
  228. {
  229. *value = dev->vendor;
  230. return 0;
  231. }
  232. static int xen_pcibk_read_device(struct pci_dev *dev, int offset,
  233. u16 *value, void *data)
  234. {
  235. *value = dev->device;
  236. return 0;
  237. }
  238. static int interrupt_read(struct pci_dev *dev, int offset, u8 * value,
  239. void *data)
  240. {
  241. *value = (u8) dev->irq;
  242. return 0;
  243. }
  244. static int bist_write(struct pci_dev *dev, int offset, u8 value, void *data)
  245. {
  246. u8 cur_value;
  247. int err;
  248. err = pci_read_config_byte(dev, offset, &cur_value);
  249. if (err)
  250. goto out;
  251. if ((cur_value & ~PCI_BIST_START) == (value & ~PCI_BIST_START)
  252. || value == PCI_BIST_START)
  253. err = pci_write_config_byte(dev, offset, value);
  254. out:
  255. return err;
  256. }
  257. static const struct config_field header_common[] = {
  258. {
  259. .offset = PCI_VENDOR_ID,
  260. .size = 2,
  261. .u.w.read = xen_pcibk_read_vendor,
  262. },
  263. {
  264. .offset = PCI_DEVICE_ID,
  265. .size = 2,
  266. .u.w.read = xen_pcibk_read_device,
  267. },
  268. {
  269. .offset = PCI_COMMAND,
  270. .size = 2,
  271. .init = command_init,
  272. .release = bar_release,
  273. .u.w.read = command_read,
  274. .u.w.write = command_write,
  275. },
  276. {
  277. .offset = PCI_INTERRUPT_LINE,
  278. .size = 1,
  279. .u.b.read = interrupt_read,
  280. },
  281. {
  282. .offset = PCI_INTERRUPT_PIN,
  283. .size = 1,
  284. .u.b.read = xen_pcibk_read_config_byte,
  285. },
  286. {
  287. /* Any side effects of letting driver domain control cache line? */
  288. .offset = PCI_CACHE_LINE_SIZE,
  289. .size = 1,
  290. .u.b.read = xen_pcibk_read_config_byte,
  291. .u.b.write = xen_pcibk_write_config_byte,
  292. },
  293. {
  294. .offset = PCI_LATENCY_TIMER,
  295. .size = 1,
  296. .u.b.read = xen_pcibk_read_config_byte,
  297. },
  298. {
  299. .offset = PCI_BIST,
  300. .size = 1,
  301. .u.b.read = xen_pcibk_read_config_byte,
  302. .u.b.write = bist_write,
  303. },
  304. {}
  305. };
  306. #define CFG_FIELD_BAR(reg_offset) \
  307. { \
  308. .offset = reg_offset, \
  309. .size = 4, \
  310. .init = bar_init, \
  311. .reset = bar_reset, \
  312. .release = bar_release, \
  313. .u.dw.read = bar_read, \
  314. .u.dw.write = bar_write, \
  315. }
  316. #define CFG_FIELD_ROM(reg_offset) \
  317. { \
  318. .offset = reg_offset, \
  319. .size = 4, \
  320. .init = rom_init, \
  321. .reset = bar_reset, \
  322. .release = bar_release, \
  323. .u.dw.read = bar_read, \
  324. .u.dw.write = rom_write, \
  325. }
  326. static const struct config_field header_0[] = {
  327. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  328. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  329. CFG_FIELD_BAR(PCI_BASE_ADDRESS_2),
  330. CFG_FIELD_BAR(PCI_BASE_ADDRESS_3),
  331. CFG_FIELD_BAR(PCI_BASE_ADDRESS_4),
  332. CFG_FIELD_BAR(PCI_BASE_ADDRESS_5),
  333. CFG_FIELD_ROM(PCI_ROM_ADDRESS),
  334. {}
  335. };
  336. static const struct config_field header_1[] = {
  337. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  338. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  339. CFG_FIELD_ROM(PCI_ROM_ADDRESS1),
  340. {}
  341. };
  342. int xen_pcibk_config_header_add_fields(struct pci_dev *dev)
  343. {
  344. int err;
  345. err = xen_pcibk_config_add_fields(dev, header_common);
  346. if (err)
  347. goto out;
  348. switch (dev->hdr_type) {
  349. case PCI_HEADER_TYPE_NORMAL:
  350. err = xen_pcibk_config_add_fields(dev, header_0);
  351. break;
  352. case PCI_HEADER_TYPE_BRIDGE:
  353. err = xen_pcibk_config_add_fields(dev, header_1);
  354. break;
  355. default:
  356. err = -EINVAL;
  357. pr_err("%s: Unsupported header type %d!\n",
  358. pci_name(dev), dev->hdr_type);
  359. break;
  360. }
  361. out:
  362. return err;
  363. }