uncore_snb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Nehalem/SandBridge/Haswell/Broadwell/Skylake uncore support */
  3. #include "uncore.h"
  4. /* Uncore IMC PCI IDs */
  5. #define PCI_DEVICE_ID_INTEL_SNB_IMC 0x0100
  6. #define PCI_DEVICE_ID_INTEL_IVB_IMC 0x0154
  7. #define PCI_DEVICE_ID_INTEL_IVB_E3_IMC 0x0150
  8. #define PCI_DEVICE_ID_INTEL_HSW_IMC 0x0c00
  9. #define PCI_DEVICE_ID_INTEL_HSW_U_IMC 0x0a04
  10. #define PCI_DEVICE_ID_INTEL_BDW_IMC 0x1604
  11. #define PCI_DEVICE_ID_INTEL_SKL_U_IMC 0x1904
  12. #define PCI_DEVICE_ID_INTEL_SKL_Y_IMC 0x190c
  13. #define PCI_DEVICE_ID_INTEL_SKL_HD_IMC 0x1900
  14. #define PCI_DEVICE_ID_INTEL_SKL_HQ_IMC 0x1910
  15. #define PCI_DEVICE_ID_INTEL_SKL_SD_IMC 0x190f
  16. #define PCI_DEVICE_ID_INTEL_SKL_SQ_IMC 0x191f
  17. #define PCI_DEVICE_ID_INTEL_KBL_Y_IMC 0x590c
  18. #define PCI_DEVICE_ID_INTEL_KBL_U_IMC 0x5904
  19. #define PCI_DEVICE_ID_INTEL_KBL_UQ_IMC 0x5914
  20. #define PCI_DEVICE_ID_INTEL_KBL_SD_IMC 0x590f
  21. #define PCI_DEVICE_ID_INTEL_KBL_SQ_IMC 0x591f
  22. #define PCI_DEVICE_ID_INTEL_CFL_2U_IMC 0x3ecc
  23. #define PCI_DEVICE_ID_INTEL_CFL_4U_IMC 0x3ed0
  24. #define PCI_DEVICE_ID_INTEL_CFL_4H_IMC 0x3e10
  25. #define PCI_DEVICE_ID_INTEL_CFL_6H_IMC 0x3ec4
  26. #define PCI_DEVICE_ID_INTEL_CFL_2S_D_IMC 0x3e0f
  27. #define PCI_DEVICE_ID_INTEL_CFL_4S_D_IMC 0x3e1f
  28. #define PCI_DEVICE_ID_INTEL_CFL_6S_D_IMC 0x3ec2
  29. #define PCI_DEVICE_ID_INTEL_CFL_8S_D_IMC 0x3e30
  30. #define PCI_DEVICE_ID_INTEL_CFL_4S_W_IMC 0x3e18
  31. #define PCI_DEVICE_ID_INTEL_CFL_6S_W_IMC 0x3ec6
  32. #define PCI_DEVICE_ID_INTEL_CFL_8S_W_IMC 0x3e31
  33. #define PCI_DEVICE_ID_INTEL_CFL_4S_S_IMC 0x3e33
  34. #define PCI_DEVICE_ID_INTEL_CFL_6S_S_IMC 0x3eca
  35. #define PCI_DEVICE_ID_INTEL_CFL_8S_S_IMC 0x3e32
  36. /* SNB event control */
  37. #define SNB_UNC_CTL_EV_SEL_MASK 0x000000ff
  38. #define SNB_UNC_CTL_UMASK_MASK 0x0000ff00
  39. #define SNB_UNC_CTL_EDGE_DET (1 << 18)
  40. #define SNB_UNC_CTL_EN (1 << 22)
  41. #define SNB_UNC_CTL_INVERT (1 << 23)
  42. #define SNB_UNC_CTL_CMASK_MASK 0x1f000000
  43. #define NHM_UNC_CTL_CMASK_MASK 0xff000000
  44. #define NHM_UNC_FIXED_CTR_CTL_EN (1 << 0)
  45. #define SNB_UNC_RAW_EVENT_MASK (SNB_UNC_CTL_EV_SEL_MASK | \
  46. SNB_UNC_CTL_UMASK_MASK | \
  47. SNB_UNC_CTL_EDGE_DET | \
  48. SNB_UNC_CTL_INVERT | \
  49. SNB_UNC_CTL_CMASK_MASK)
  50. #define NHM_UNC_RAW_EVENT_MASK (SNB_UNC_CTL_EV_SEL_MASK | \
  51. SNB_UNC_CTL_UMASK_MASK | \
  52. SNB_UNC_CTL_EDGE_DET | \
  53. SNB_UNC_CTL_INVERT | \
  54. NHM_UNC_CTL_CMASK_MASK)
  55. /* SNB global control register */
  56. #define SNB_UNC_PERF_GLOBAL_CTL 0x391
  57. #define SNB_UNC_FIXED_CTR_CTRL 0x394
  58. #define SNB_UNC_FIXED_CTR 0x395
  59. /* SNB uncore global control */
  60. #define SNB_UNC_GLOBAL_CTL_CORE_ALL ((1 << 4) - 1)
  61. #define SNB_UNC_GLOBAL_CTL_EN (1 << 29)
  62. /* SNB Cbo register */
  63. #define SNB_UNC_CBO_0_PERFEVTSEL0 0x700
  64. #define SNB_UNC_CBO_0_PER_CTR0 0x706
  65. #define SNB_UNC_CBO_MSR_OFFSET 0x10
  66. /* SNB ARB register */
  67. #define SNB_UNC_ARB_PER_CTR0 0x3b0
  68. #define SNB_UNC_ARB_PERFEVTSEL0 0x3b2
  69. #define SNB_UNC_ARB_MSR_OFFSET 0x10
  70. /* NHM global control register */
  71. #define NHM_UNC_PERF_GLOBAL_CTL 0x391
  72. #define NHM_UNC_FIXED_CTR 0x394
  73. #define NHM_UNC_FIXED_CTR_CTRL 0x395
  74. /* NHM uncore global control */
  75. #define NHM_UNC_GLOBAL_CTL_EN_PC_ALL ((1ULL << 8) - 1)
  76. #define NHM_UNC_GLOBAL_CTL_EN_FC (1ULL << 32)
  77. /* NHM uncore register */
  78. #define NHM_UNC_PERFEVTSEL0 0x3c0
  79. #define NHM_UNC_UNCORE_PMC0 0x3b0
  80. /* SKL uncore global control */
  81. #define SKL_UNC_PERF_GLOBAL_CTL 0xe01
  82. #define SKL_UNC_GLOBAL_CTL_CORE_ALL ((1 << 5) - 1)
  83. DEFINE_UNCORE_FORMAT_ATTR(event, event, "config:0-7");
  84. DEFINE_UNCORE_FORMAT_ATTR(umask, umask, "config:8-15");
  85. DEFINE_UNCORE_FORMAT_ATTR(edge, edge, "config:18");
  86. DEFINE_UNCORE_FORMAT_ATTR(inv, inv, "config:23");
  87. DEFINE_UNCORE_FORMAT_ATTR(cmask5, cmask, "config:24-28");
  88. DEFINE_UNCORE_FORMAT_ATTR(cmask8, cmask, "config:24-31");
  89. /* Sandy Bridge uncore support */
  90. static void snb_uncore_msr_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  91. {
  92. struct hw_perf_event *hwc = &event->hw;
  93. if (hwc->idx < UNCORE_PMC_IDX_FIXED)
  94. wrmsrl(hwc->config_base, hwc->config | SNB_UNC_CTL_EN);
  95. else
  96. wrmsrl(hwc->config_base, SNB_UNC_CTL_EN);
  97. }
  98. static void snb_uncore_msr_disable_event(struct intel_uncore_box *box, struct perf_event *event)
  99. {
  100. wrmsrl(event->hw.config_base, 0);
  101. }
  102. static void snb_uncore_msr_init_box(struct intel_uncore_box *box)
  103. {
  104. if (box->pmu->pmu_idx == 0) {
  105. wrmsrl(SNB_UNC_PERF_GLOBAL_CTL,
  106. SNB_UNC_GLOBAL_CTL_EN | SNB_UNC_GLOBAL_CTL_CORE_ALL);
  107. }
  108. }
  109. static void snb_uncore_msr_enable_box(struct intel_uncore_box *box)
  110. {
  111. wrmsrl(SNB_UNC_PERF_GLOBAL_CTL,
  112. SNB_UNC_GLOBAL_CTL_EN | SNB_UNC_GLOBAL_CTL_CORE_ALL);
  113. }
  114. static void snb_uncore_msr_exit_box(struct intel_uncore_box *box)
  115. {
  116. if (box->pmu->pmu_idx == 0)
  117. wrmsrl(SNB_UNC_PERF_GLOBAL_CTL, 0);
  118. }
  119. static struct uncore_event_desc snb_uncore_events[] = {
  120. INTEL_UNCORE_EVENT_DESC(clockticks, "event=0xff,umask=0x00"),
  121. { /* end: all zeroes */ },
  122. };
  123. static struct attribute *snb_uncore_formats_attr[] = {
  124. &format_attr_event.attr,
  125. &format_attr_umask.attr,
  126. &format_attr_edge.attr,
  127. &format_attr_inv.attr,
  128. &format_attr_cmask5.attr,
  129. NULL,
  130. };
  131. static const struct attribute_group snb_uncore_format_group = {
  132. .name = "format",
  133. .attrs = snb_uncore_formats_attr,
  134. };
  135. static struct intel_uncore_ops snb_uncore_msr_ops = {
  136. .init_box = snb_uncore_msr_init_box,
  137. .enable_box = snb_uncore_msr_enable_box,
  138. .exit_box = snb_uncore_msr_exit_box,
  139. .disable_event = snb_uncore_msr_disable_event,
  140. .enable_event = snb_uncore_msr_enable_event,
  141. .read_counter = uncore_msr_read_counter,
  142. };
  143. static struct event_constraint snb_uncore_arb_constraints[] = {
  144. UNCORE_EVENT_CONSTRAINT(0x80, 0x1),
  145. UNCORE_EVENT_CONSTRAINT(0x83, 0x1),
  146. EVENT_CONSTRAINT_END
  147. };
  148. static struct intel_uncore_type snb_uncore_cbox = {
  149. .name = "cbox",
  150. .num_counters = 2,
  151. .num_boxes = 4,
  152. .perf_ctr_bits = 44,
  153. .fixed_ctr_bits = 48,
  154. .perf_ctr = SNB_UNC_CBO_0_PER_CTR0,
  155. .event_ctl = SNB_UNC_CBO_0_PERFEVTSEL0,
  156. .fixed_ctr = SNB_UNC_FIXED_CTR,
  157. .fixed_ctl = SNB_UNC_FIXED_CTR_CTRL,
  158. .single_fixed = 1,
  159. .event_mask = SNB_UNC_RAW_EVENT_MASK,
  160. .msr_offset = SNB_UNC_CBO_MSR_OFFSET,
  161. .ops = &snb_uncore_msr_ops,
  162. .format_group = &snb_uncore_format_group,
  163. .event_descs = snb_uncore_events,
  164. };
  165. static struct intel_uncore_type snb_uncore_arb = {
  166. .name = "arb",
  167. .num_counters = 2,
  168. .num_boxes = 1,
  169. .perf_ctr_bits = 44,
  170. .perf_ctr = SNB_UNC_ARB_PER_CTR0,
  171. .event_ctl = SNB_UNC_ARB_PERFEVTSEL0,
  172. .event_mask = SNB_UNC_RAW_EVENT_MASK,
  173. .msr_offset = SNB_UNC_ARB_MSR_OFFSET,
  174. .constraints = snb_uncore_arb_constraints,
  175. .ops = &snb_uncore_msr_ops,
  176. .format_group = &snb_uncore_format_group,
  177. };
  178. static struct intel_uncore_type *snb_msr_uncores[] = {
  179. &snb_uncore_cbox,
  180. &snb_uncore_arb,
  181. NULL,
  182. };
  183. void snb_uncore_cpu_init(void)
  184. {
  185. uncore_msr_uncores = snb_msr_uncores;
  186. if (snb_uncore_cbox.num_boxes > boot_cpu_data.x86_max_cores)
  187. snb_uncore_cbox.num_boxes = boot_cpu_data.x86_max_cores;
  188. }
  189. static void skl_uncore_msr_init_box(struct intel_uncore_box *box)
  190. {
  191. if (box->pmu->pmu_idx == 0) {
  192. wrmsrl(SKL_UNC_PERF_GLOBAL_CTL,
  193. SNB_UNC_GLOBAL_CTL_EN | SKL_UNC_GLOBAL_CTL_CORE_ALL);
  194. }
  195. /* The 8th CBOX has different MSR space */
  196. if (box->pmu->pmu_idx == 7)
  197. __set_bit(UNCORE_BOX_FLAG_CFL8_CBOX_MSR_OFFS, &box->flags);
  198. }
  199. static void skl_uncore_msr_enable_box(struct intel_uncore_box *box)
  200. {
  201. wrmsrl(SKL_UNC_PERF_GLOBAL_CTL,
  202. SNB_UNC_GLOBAL_CTL_EN | SKL_UNC_GLOBAL_CTL_CORE_ALL);
  203. }
  204. static void skl_uncore_msr_exit_box(struct intel_uncore_box *box)
  205. {
  206. if (box->pmu->pmu_idx == 0)
  207. wrmsrl(SKL_UNC_PERF_GLOBAL_CTL, 0);
  208. }
  209. static struct intel_uncore_ops skl_uncore_msr_ops = {
  210. .init_box = skl_uncore_msr_init_box,
  211. .enable_box = skl_uncore_msr_enable_box,
  212. .exit_box = skl_uncore_msr_exit_box,
  213. .disable_event = snb_uncore_msr_disable_event,
  214. .enable_event = snb_uncore_msr_enable_event,
  215. .read_counter = uncore_msr_read_counter,
  216. };
  217. static struct intel_uncore_type skl_uncore_cbox = {
  218. .name = "cbox",
  219. .num_counters = 4,
  220. .num_boxes = 8,
  221. .perf_ctr_bits = 44,
  222. .fixed_ctr_bits = 48,
  223. .perf_ctr = SNB_UNC_CBO_0_PER_CTR0,
  224. .event_ctl = SNB_UNC_CBO_0_PERFEVTSEL0,
  225. .fixed_ctr = SNB_UNC_FIXED_CTR,
  226. .fixed_ctl = SNB_UNC_FIXED_CTR_CTRL,
  227. .single_fixed = 1,
  228. .event_mask = SNB_UNC_RAW_EVENT_MASK,
  229. .msr_offset = SNB_UNC_CBO_MSR_OFFSET,
  230. .ops = &skl_uncore_msr_ops,
  231. .format_group = &snb_uncore_format_group,
  232. .event_descs = snb_uncore_events,
  233. };
  234. static struct intel_uncore_type *skl_msr_uncores[] = {
  235. &skl_uncore_cbox,
  236. &snb_uncore_arb,
  237. NULL,
  238. };
  239. void skl_uncore_cpu_init(void)
  240. {
  241. uncore_msr_uncores = skl_msr_uncores;
  242. if (skl_uncore_cbox.num_boxes > boot_cpu_data.x86_max_cores)
  243. skl_uncore_cbox.num_boxes = boot_cpu_data.x86_max_cores;
  244. snb_uncore_arb.ops = &skl_uncore_msr_ops;
  245. }
  246. enum {
  247. SNB_PCI_UNCORE_IMC,
  248. };
  249. static struct uncore_event_desc snb_uncore_imc_events[] = {
  250. INTEL_UNCORE_EVENT_DESC(data_reads, "event=0x01"),
  251. INTEL_UNCORE_EVENT_DESC(data_reads.scale, "6.103515625e-5"),
  252. INTEL_UNCORE_EVENT_DESC(data_reads.unit, "MiB"),
  253. INTEL_UNCORE_EVENT_DESC(data_writes, "event=0x02"),
  254. INTEL_UNCORE_EVENT_DESC(data_writes.scale, "6.103515625e-5"),
  255. INTEL_UNCORE_EVENT_DESC(data_writes.unit, "MiB"),
  256. { /* end: all zeroes */ },
  257. };
  258. #define SNB_UNCORE_PCI_IMC_EVENT_MASK 0xff
  259. #define SNB_UNCORE_PCI_IMC_BAR_OFFSET 0x48
  260. /* page size multiple covering all config regs */
  261. #define SNB_UNCORE_PCI_IMC_MAP_SIZE 0x6000
  262. #define SNB_UNCORE_PCI_IMC_DATA_READS 0x1
  263. #define SNB_UNCORE_PCI_IMC_DATA_READS_BASE 0x5050
  264. #define SNB_UNCORE_PCI_IMC_DATA_WRITES 0x2
  265. #define SNB_UNCORE_PCI_IMC_DATA_WRITES_BASE 0x5054
  266. #define SNB_UNCORE_PCI_IMC_CTR_BASE SNB_UNCORE_PCI_IMC_DATA_READS_BASE
  267. enum perf_snb_uncore_imc_freerunning_types {
  268. SNB_PCI_UNCORE_IMC_DATA = 0,
  269. SNB_PCI_UNCORE_IMC_FREERUNNING_TYPE_MAX,
  270. };
  271. static struct freerunning_counters snb_uncore_imc_freerunning[] = {
  272. [SNB_PCI_UNCORE_IMC_DATA] = { SNB_UNCORE_PCI_IMC_DATA_READS_BASE, 0x4, 0x0, 2, 32 },
  273. };
  274. static struct attribute *snb_uncore_imc_formats_attr[] = {
  275. &format_attr_event.attr,
  276. NULL,
  277. };
  278. static const struct attribute_group snb_uncore_imc_format_group = {
  279. .name = "format",
  280. .attrs = snb_uncore_imc_formats_attr,
  281. };
  282. static void snb_uncore_imc_init_box(struct intel_uncore_box *box)
  283. {
  284. struct pci_dev *pdev = box->pci_dev;
  285. int where = SNB_UNCORE_PCI_IMC_BAR_OFFSET;
  286. resource_size_t addr;
  287. u32 pci_dword;
  288. pci_read_config_dword(pdev, where, &pci_dword);
  289. addr = pci_dword;
  290. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  291. pci_read_config_dword(pdev, where + 4, &pci_dword);
  292. addr |= ((resource_size_t)pci_dword << 32);
  293. #endif
  294. addr &= ~(PAGE_SIZE - 1);
  295. box->io_addr = ioremap(addr, SNB_UNCORE_PCI_IMC_MAP_SIZE);
  296. box->hrtimer_duration = UNCORE_SNB_IMC_HRTIMER_INTERVAL;
  297. }
  298. static void snb_uncore_imc_exit_box(struct intel_uncore_box *box)
  299. {
  300. iounmap(box->io_addr);
  301. }
  302. static void snb_uncore_imc_enable_box(struct intel_uncore_box *box)
  303. {}
  304. static void snb_uncore_imc_disable_box(struct intel_uncore_box *box)
  305. {}
  306. static void snb_uncore_imc_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  307. {}
  308. static void snb_uncore_imc_disable_event(struct intel_uncore_box *box, struct perf_event *event)
  309. {}
  310. static u64 snb_uncore_imc_read_counter(struct intel_uncore_box *box, struct perf_event *event)
  311. {
  312. struct hw_perf_event *hwc = &event->hw;
  313. return (u64)*(unsigned int *)(box->io_addr + hwc->event_base);
  314. }
  315. /*
  316. * Keep the custom event_init() function compatible with old event
  317. * encoding for free running counters.
  318. */
  319. static int snb_uncore_imc_event_init(struct perf_event *event)
  320. {
  321. struct intel_uncore_pmu *pmu;
  322. struct intel_uncore_box *box;
  323. struct hw_perf_event *hwc = &event->hw;
  324. u64 cfg = event->attr.config & SNB_UNCORE_PCI_IMC_EVENT_MASK;
  325. int idx, base;
  326. if (event->attr.type != event->pmu->type)
  327. return -ENOENT;
  328. pmu = uncore_event_to_pmu(event);
  329. /* no device found for this pmu */
  330. if (pmu->func_id < 0)
  331. return -ENOENT;
  332. /* Sampling not supported yet */
  333. if (hwc->sample_period)
  334. return -EINVAL;
  335. /* unsupported modes and filters */
  336. if (event->attr.exclude_user ||
  337. event->attr.exclude_kernel ||
  338. event->attr.exclude_hv ||
  339. event->attr.exclude_idle ||
  340. event->attr.exclude_host ||
  341. event->attr.exclude_guest ||
  342. event->attr.sample_period) /* no sampling */
  343. return -EINVAL;
  344. /*
  345. * Place all uncore events for a particular physical package
  346. * onto a single cpu
  347. */
  348. if (event->cpu < 0)
  349. return -EINVAL;
  350. /* check only supported bits are set */
  351. if (event->attr.config & ~SNB_UNCORE_PCI_IMC_EVENT_MASK)
  352. return -EINVAL;
  353. box = uncore_pmu_to_box(pmu, event->cpu);
  354. if (!box || box->cpu < 0)
  355. return -EINVAL;
  356. event->cpu = box->cpu;
  357. event->pmu_private = box;
  358. event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
  359. event->hw.idx = -1;
  360. event->hw.last_tag = ~0ULL;
  361. event->hw.extra_reg.idx = EXTRA_REG_NONE;
  362. event->hw.branch_reg.idx = EXTRA_REG_NONE;
  363. /*
  364. * check event is known (whitelist, determines counter)
  365. */
  366. switch (cfg) {
  367. case SNB_UNCORE_PCI_IMC_DATA_READS:
  368. base = SNB_UNCORE_PCI_IMC_DATA_READS_BASE;
  369. idx = UNCORE_PMC_IDX_FREERUNNING;
  370. break;
  371. case SNB_UNCORE_PCI_IMC_DATA_WRITES:
  372. base = SNB_UNCORE_PCI_IMC_DATA_WRITES_BASE;
  373. idx = UNCORE_PMC_IDX_FREERUNNING;
  374. break;
  375. default:
  376. return -EINVAL;
  377. }
  378. /* must be done before validate_group */
  379. event->hw.event_base = base;
  380. event->hw.config = cfg;
  381. event->hw.idx = idx;
  382. /* no group validation needed, we have free running counters */
  383. return 0;
  384. }
  385. static int snb_uncore_imc_hw_config(struct intel_uncore_box *box, struct perf_event *event)
  386. {
  387. return 0;
  388. }
  389. int snb_pci2phy_map_init(int devid)
  390. {
  391. struct pci_dev *dev = NULL;
  392. struct pci2phy_map *map;
  393. int bus, segment;
  394. dev = pci_get_device(PCI_VENDOR_ID_INTEL, devid, dev);
  395. if (!dev)
  396. return -ENOTTY;
  397. bus = dev->bus->number;
  398. segment = pci_domain_nr(dev->bus);
  399. raw_spin_lock(&pci2phy_map_lock);
  400. map = __find_pci2phy_map(segment);
  401. if (!map) {
  402. raw_spin_unlock(&pci2phy_map_lock);
  403. pci_dev_put(dev);
  404. return -ENOMEM;
  405. }
  406. map->pbus_to_physid[bus] = 0;
  407. raw_spin_unlock(&pci2phy_map_lock);
  408. pci_dev_put(dev);
  409. return 0;
  410. }
  411. static struct pmu snb_uncore_imc_pmu = {
  412. .task_ctx_nr = perf_invalid_context,
  413. .event_init = snb_uncore_imc_event_init,
  414. .add = uncore_pmu_event_add,
  415. .del = uncore_pmu_event_del,
  416. .start = uncore_pmu_event_start,
  417. .stop = uncore_pmu_event_stop,
  418. .read = uncore_pmu_event_read,
  419. };
  420. static struct intel_uncore_ops snb_uncore_imc_ops = {
  421. .init_box = snb_uncore_imc_init_box,
  422. .exit_box = snb_uncore_imc_exit_box,
  423. .enable_box = snb_uncore_imc_enable_box,
  424. .disable_box = snb_uncore_imc_disable_box,
  425. .disable_event = snb_uncore_imc_disable_event,
  426. .enable_event = snb_uncore_imc_enable_event,
  427. .hw_config = snb_uncore_imc_hw_config,
  428. .read_counter = snb_uncore_imc_read_counter,
  429. };
  430. static struct intel_uncore_type snb_uncore_imc = {
  431. .name = "imc",
  432. .num_counters = 2,
  433. .num_boxes = 1,
  434. .num_freerunning_types = SNB_PCI_UNCORE_IMC_FREERUNNING_TYPE_MAX,
  435. .freerunning = snb_uncore_imc_freerunning,
  436. .event_descs = snb_uncore_imc_events,
  437. .format_group = &snb_uncore_imc_format_group,
  438. .ops = &snb_uncore_imc_ops,
  439. .pmu = &snb_uncore_imc_pmu,
  440. };
  441. static struct intel_uncore_type *snb_pci_uncores[] = {
  442. [SNB_PCI_UNCORE_IMC] = &snb_uncore_imc,
  443. NULL,
  444. };
  445. static const struct pci_device_id snb_uncore_pci_ids[] = {
  446. { /* IMC */
  447. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SNB_IMC),
  448. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  449. },
  450. { /* end: all zeroes */ },
  451. };
  452. static const struct pci_device_id ivb_uncore_pci_ids[] = {
  453. { /* IMC */
  454. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IVB_IMC),
  455. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  456. },
  457. { /* IMC */
  458. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IVB_E3_IMC),
  459. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  460. },
  461. { /* end: all zeroes */ },
  462. };
  463. static const struct pci_device_id hsw_uncore_pci_ids[] = {
  464. { /* IMC */
  465. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HSW_IMC),
  466. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  467. },
  468. { /* IMC */
  469. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_HSW_U_IMC),
  470. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  471. },
  472. { /* end: all zeroes */ },
  473. };
  474. static const struct pci_device_id bdw_uncore_pci_ids[] = {
  475. { /* IMC */
  476. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BDW_IMC),
  477. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  478. },
  479. { /* end: all zeroes */ },
  480. };
  481. static const struct pci_device_id skl_uncore_pci_ids[] = {
  482. { /* IMC */
  483. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SKL_Y_IMC),
  484. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  485. },
  486. { /* IMC */
  487. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SKL_U_IMC),
  488. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  489. },
  490. { /* IMC */
  491. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SKL_HD_IMC),
  492. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  493. },
  494. { /* IMC */
  495. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SKL_HQ_IMC),
  496. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  497. },
  498. { /* IMC */
  499. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SKL_SD_IMC),
  500. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  501. },
  502. { /* IMC */
  503. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SKL_SQ_IMC),
  504. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  505. },
  506. { /* IMC */
  507. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KBL_Y_IMC),
  508. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  509. },
  510. { /* IMC */
  511. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KBL_U_IMC),
  512. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  513. },
  514. { /* IMC */
  515. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KBL_UQ_IMC),
  516. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  517. },
  518. { /* IMC */
  519. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KBL_SD_IMC),
  520. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  521. },
  522. { /* IMC */
  523. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KBL_SQ_IMC),
  524. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  525. },
  526. { /* IMC */
  527. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_2U_IMC),
  528. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  529. },
  530. { /* IMC */
  531. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_4U_IMC),
  532. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  533. },
  534. { /* IMC */
  535. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_4H_IMC),
  536. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  537. },
  538. { /* IMC */
  539. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_6H_IMC),
  540. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  541. },
  542. { /* IMC */
  543. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_2S_D_IMC),
  544. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  545. },
  546. { /* IMC */
  547. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_4S_D_IMC),
  548. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  549. },
  550. { /* IMC */
  551. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_6S_D_IMC),
  552. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  553. },
  554. { /* IMC */
  555. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_8S_D_IMC),
  556. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  557. },
  558. { /* IMC */
  559. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_4S_W_IMC),
  560. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  561. },
  562. { /* IMC */
  563. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_6S_W_IMC),
  564. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  565. },
  566. { /* IMC */
  567. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_8S_W_IMC),
  568. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  569. },
  570. { /* IMC */
  571. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_4S_S_IMC),
  572. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  573. },
  574. { /* IMC */
  575. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_6S_S_IMC),
  576. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  577. },
  578. { /* IMC */
  579. PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CFL_8S_S_IMC),
  580. .driver_data = UNCORE_PCI_DEV_DATA(SNB_PCI_UNCORE_IMC, 0),
  581. },
  582. { /* end: all zeroes */ },
  583. };
  584. static struct pci_driver snb_uncore_pci_driver = {
  585. .name = "snb_uncore",
  586. .id_table = snb_uncore_pci_ids,
  587. };
  588. static struct pci_driver ivb_uncore_pci_driver = {
  589. .name = "ivb_uncore",
  590. .id_table = ivb_uncore_pci_ids,
  591. };
  592. static struct pci_driver hsw_uncore_pci_driver = {
  593. .name = "hsw_uncore",
  594. .id_table = hsw_uncore_pci_ids,
  595. };
  596. static struct pci_driver bdw_uncore_pci_driver = {
  597. .name = "bdw_uncore",
  598. .id_table = bdw_uncore_pci_ids,
  599. };
  600. static struct pci_driver skl_uncore_pci_driver = {
  601. .name = "skl_uncore",
  602. .id_table = skl_uncore_pci_ids,
  603. };
  604. struct imc_uncore_pci_dev {
  605. __u32 pci_id;
  606. struct pci_driver *driver;
  607. };
  608. #define IMC_DEV(a, d) \
  609. { .pci_id = PCI_DEVICE_ID_INTEL_##a, .driver = (d) }
  610. static const struct imc_uncore_pci_dev desktop_imc_pci_ids[] = {
  611. IMC_DEV(SNB_IMC, &snb_uncore_pci_driver),
  612. IMC_DEV(IVB_IMC, &ivb_uncore_pci_driver), /* 3rd Gen Core processor */
  613. IMC_DEV(IVB_E3_IMC, &ivb_uncore_pci_driver), /* Xeon E3-1200 v2/3rd Gen Core processor */
  614. IMC_DEV(HSW_IMC, &hsw_uncore_pci_driver), /* 4th Gen Core Processor */
  615. IMC_DEV(HSW_U_IMC, &hsw_uncore_pci_driver), /* 4th Gen Core ULT Mobile Processor */
  616. IMC_DEV(BDW_IMC, &bdw_uncore_pci_driver), /* 5th Gen Core U */
  617. IMC_DEV(SKL_Y_IMC, &skl_uncore_pci_driver), /* 6th Gen Core Y */
  618. IMC_DEV(SKL_U_IMC, &skl_uncore_pci_driver), /* 6th Gen Core U */
  619. IMC_DEV(SKL_HD_IMC, &skl_uncore_pci_driver), /* 6th Gen Core H Dual Core */
  620. IMC_DEV(SKL_HQ_IMC, &skl_uncore_pci_driver), /* 6th Gen Core H Quad Core */
  621. IMC_DEV(SKL_SD_IMC, &skl_uncore_pci_driver), /* 6th Gen Core S Dual Core */
  622. IMC_DEV(SKL_SQ_IMC, &skl_uncore_pci_driver), /* 6th Gen Core S Quad Core */
  623. IMC_DEV(KBL_Y_IMC, &skl_uncore_pci_driver), /* 7th Gen Core Y */
  624. IMC_DEV(KBL_U_IMC, &skl_uncore_pci_driver), /* 7th Gen Core U */
  625. IMC_DEV(KBL_UQ_IMC, &skl_uncore_pci_driver), /* 7th Gen Core U Quad Core */
  626. IMC_DEV(KBL_SD_IMC, &skl_uncore_pci_driver), /* 7th Gen Core S Dual Core */
  627. IMC_DEV(KBL_SQ_IMC, &skl_uncore_pci_driver), /* 7th Gen Core S Quad Core */
  628. IMC_DEV(CFL_2U_IMC, &skl_uncore_pci_driver), /* 8th Gen Core U 2 Cores */
  629. IMC_DEV(CFL_4U_IMC, &skl_uncore_pci_driver), /* 8th Gen Core U 4 Cores */
  630. IMC_DEV(CFL_4H_IMC, &skl_uncore_pci_driver), /* 8th Gen Core H 4 Cores */
  631. IMC_DEV(CFL_6H_IMC, &skl_uncore_pci_driver), /* 8th Gen Core H 6 Cores */
  632. IMC_DEV(CFL_2S_D_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 2 Cores Desktop */
  633. IMC_DEV(CFL_4S_D_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 4 Cores Desktop */
  634. IMC_DEV(CFL_6S_D_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 6 Cores Desktop */
  635. IMC_DEV(CFL_8S_D_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 8 Cores Desktop */
  636. IMC_DEV(CFL_4S_W_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 4 Cores Work Station */
  637. IMC_DEV(CFL_6S_W_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 6 Cores Work Station */
  638. IMC_DEV(CFL_8S_W_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 8 Cores Work Station */
  639. IMC_DEV(CFL_4S_S_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 4 Cores Server */
  640. IMC_DEV(CFL_6S_S_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 6 Cores Server */
  641. IMC_DEV(CFL_8S_S_IMC, &skl_uncore_pci_driver), /* 8th Gen Core S 8 Cores Server */
  642. { /* end marker */ }
  643. };
  644. #define for_each_imc_pci_id(x, t) \
  645. for (x = (t); (x)->pci_id; x++)
  646. static struct pci_driver *imc_uncore_find_dev(void)
  647. {
  648. const struct imc_uncore_pci_dev *p;
  649. int ret;
  650. for_each_imc_pci_id(p, desktop_imc_pci_ids) {
  651. ret = snb_pci2phy_map_init(p->pci_id);
  652. if (ret == 0)
  653. return p->driver;
  654. }
  655. return NULL;
  656. }
  657. static int imc_uncore_pci_init(void)
  658. {
  659. struct pci_driver *imc_drv = imc_uncore_find_dev();
  660. if (!imc_drv)
  661. return -ENODEV;
  662. uncore_pci_uncores = snb_pci_uncores;
  663. uncore_pci_driver = imc_drv;
  664. return 0;
  665. }
  666. int snb_uncore_pci_init(void)
  667. {
  668. return imc_uncore_pci_init();
  669. }
  670. int ivb_uncore_pci_init(void)
  671. {
  672. return imc_uncore_pci_init();
  673. }
  674. int hsw_uncore_pci_init(void)
  675. {
  676. return imc_uncore_pci_init();
  677. }
  678. int bdw_uncore_pci_init(void)
  679. {
  680. return imc_uncore_pci_init();
  681. }
  682. int skl_uncore_pci_init(void)
  683. {
  684. return imc_uncore_pci_init();
  685. }
  686. /* end of Sandy Bridge uncore support */
  687. /* Nehalem uncore support */
  688. static void nhm_uncore_msr_disable_box(struct intel_uncore_box *box)
  689. {
  690. wrmsrl(NHM_UNC_PERF_GLOBAL_CTL, 0);
  691. }
  692. static void nhm_uncore_msr_enable_box(struct intel_uncore_box *box)
  693. {
  694. wrmsrl(NHM_UNC_PERF_GLOBAL_CTL, NHM_UNC_GLOBAL_CTL_EN_PC_ALL | NHM_UNC_GLOBAL_CTL_EN_FC);
  695. }
  696. static void nhm_uncore_msr_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  697. {
  698. struct hw_perf_event *hwc = &event->hw;
  699. if (hwc->idx < UNCORE_PMC_IDX_FIXED)
  700. wrmsrl(hwc->config_base, hwc->config | SNB_UNC_CTL_EN);
  701. else
  702. wrmsrl(hwc->config_base, NHM_UNC_FIXED_CTR_CTL_EN);
  703. }
  704. static struct attribute *nhm_uncore_formats_attr[] = {
  705. &format_attr_event.attr,
  706. &format_attr_umask.attr,
  707. &format_attr_edge.attr,
  708. &format_attr_inv.attr,
  709. &format_attr_cmask8.attr,
  710. NULL,
  711. };
  712. static const struct attribute_group nhm_uncore_format_group = {
  713. .name = "format",
  714. .attrs = nhm_uncore_formats_attr,
  715. };
  716. static struct uncore_event_desc nhm_uncore_events[] = {
  717. INTEL_UNCORE_EVENT_DESC(clockticks, "event=0xff,umask=0x00"),
  718. INTEL_UNCORE_EVENT_DESC(qmc_writes_full_any, "event=0x2f,umask=0x0f"),
  719. INTEL_UNCORE_EVENT_DESC(qmc_normal_reads_any, "event=0x2c,umask=0x0f"),
  720. INTEL_UNCORE_EVENT_DESC(qhl_request_ioh_reads, "event=0x20,umask=0x01"),
  721. INTEL_UNCORE_EVENT_DESC(qhl_request_ioh_writes, "event=0x20,umask=0x02"),
  722. INTEL_UNCORE_EVENT_DESC(qhl_request_remote_reads, "event=0x20,umask=0x04"),
  723. INTEL_UNCORE_EVENT_DESC(qhl_request_remote_writes, "event=0x20,umask=0x08"),
  724. INTEL_UNCORE_EVENT_DESC(qhl_request_local_reads, "event=0x20,umask=0x10"),
  725. INTEL_UNCORE_EVENT_DESC(qhl_request_local_writes, "event=0x20,umask=0x20"),
  726. { /* end: all zeroes */ },
  727. };
  728. static struct intel_uncore_ops nhm_uncore_msr_ops = {
  729. .disable_box = nhm_uncore_msr_disable_box,
  730. .enable_box = nhm_uncore_msr_enable_box,
  731. .disable_event = snb_uncore_msr_disable_event,
  732. .enable_event = nhm_uncore_msr_enable_event,
  733. .read_counter = uncore_msr_read_counter,
  734. };
  735. static struct intel_uncore_type nhm_uncore = {
  736. .name = "",
  737. .num_counters = 8,
  738. .num_boxes = 1,
  739. .perf_ctr_bits = 48,
  740. .fixed_ctr_bits = 48,
  741. .event_ctl = NHM_UNC_PERFEVTSEL0,
  742. .perf_ctr = NHM_UNC_UNCORE_PMC0,
  743. .fixed_ctr = NHM_UNC_FIXED_CTR,
  744. .fixed_ctl = NHM_UNC_FIXED_CTR_CTRL,
  745. .event_mask = NHM_UNC_RAW_EVENT_MASK,
  746. .event_descs = nhm_uncore_events,
  747. .ops = &nhm_uncore_msr_ops,
  748. .format_group = &nhm_uncore_format_group,
  749. };
  750. static struct intel_uncore_type *nhm_msr_uncores[] = {
  751. &nhm_uncore,
  752. NULL,
  753. };
  754. void nhm_uncore_cpu_init(void)
  755. {
  756. uncore_msr_uncores = nhm_msr_uncores;
  757. }
  758. /* end of Nehalem uncore support */