perf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Parisc performance counters
  3. * Copyright (C) 2001 Randolph Chung <tausq@debian.org>
  4. *
  5. * This code is derived, with permission, from HP/UX sources.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * Edited comment from original sources:
  23. *
  24. * This driver programs the PCX-U/PCX-W performance counters
  25. * on the PA-RISC 2.0 chips. The driver keeps all images now
  26. * internally to the kernel to hopefully eliminate the possiblity
  27. * of a bad image halting the CPU. Also, there are different
  28. * images for the PCX-W and later chips vs the PCX-U chips.
  29. *
  30. * Only 1 process is allowed to access the driver at any time,
  31. * so the only protection that is needed is at open and close.
  32. * A variable "perf_enabled" is used to hold the state of the
  33. * driver. The spinlock "perf_lock" is used to protect the
  34. * modification of the state during open/close operations so
  35. * multiple processes don't get into the driver simultaneously.
  36. *
  37. * This driver accesses the processor directly vs going through
  38. * the PDC INTRIGUE calls. This is done to eliminate bugs introduced
  39. * in various PDC revisions. The code is much more maintainable
  40. * and reliable this way vs having to debug on every version of PDC
  41. * on every box.
  42. */
  43. #include <linux/capability.h>
  44. #include <linux/init.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/miscdevice.h>
  47. #include <linux/smp_lock.h>
  48. #include <linux/spinlock.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/perf.h>
  51. #include <asm/parisc-device.h>
  52. #include <asm/processor.h>
  53. #include <asm/runway.h>
  54. #include <asm/io.h> /* for __raw_read() */
  55. #include "perf_images.h"
  56. #define MAX_RDR_WORDS 24
  57. #define PERF_VERSION 2 /* derived from hpux's PI v2 interface */
  58. /* definition of RDR regs */
  59. struct rdr_tbl_ent {
  60. uint16_t width;
  61. uint8_t num_words;
  62. uint8_t write_control;
  63. };
  64. static int perf_processor_interface __read_mostly = UNKNOWN_INTF;
  65. static int perf_enabled __read_mostly;
  66. static spinlock_t perf_lock;
  67. struct parisc_device *cpu_device __read_mostly;
  68. /* RDRs to write for PCX-W */
  69. static const int perf_rdrs_W[] =
  70. { 0, 1, 4, 5, 6, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, -1 };
  71. /* RDRs to write for PCX-U */
  72. static const int perf_rdrs_U[] =
  73. { 0, 1, 4, 5, 6, 7, 16, 17, 18, 20, 21, 22, 23, 24, 25, -1 };
  74. /* RDR register descriptions for PCX-W */
  75. static const struct rdr_tbl_ent perf_rdr_tbl_W[] = {
  76. { 19, 1, 8 }, /* RDR 0 */
  77. { 16, 1, 16 }, /* RDR 1 */
  78. { 72, 2, 0 }, /* RDR 2 */
  79. { 81, 2, 0 }, /* RDR 3 */
  80. { 328, 6, 0 }, /* RDR 4 */
  81. { 160, 3, 0 }, /* RDR 5 */
  82. { 336, 6, 0 }, /* RDR 6 */
  83. { 164, 3, 0 }, /* RDR 7 */
  84. { 0, 0, 0 }, /* RDR 8 */
  85. { 35, 1, 0 }, /* RDR 9 */
  86. { 6, 1, 0 }, /* RDR 10 */
  87. { 18, 1, 0 }, /* RDR 11 */
  88. { 13, 1, 0 }, /* RDR 12 */
  89. { 8, 1, 0 }, /* RDR 13 */
  90. { 8, 1, 0 }, /* RDR 14 */
  91. { 8, 1, 0 }, /* RDR 15 */
  92. { 1530, 24, 0 }, /* RDR 16 */
  93. { 16, 1, 0 }, /* RDR 17 */
  94. { 4, 1, 0 }, /* RDR 18 */
  95. { 0, 0, 0 }, /* RDR 19 */
  96. { 152, 3, 24 }, /* RDR 20 */
  97. { 152, 3, 24 }, /* RDR 21 */
  98. { 233, 4, 48 }, /* RDR 22 */
  99. { 233, 4, 48 }, /* RDR 23 */
  100. { 71, 2, 0 }, /* RDR 24 */
  101. { 71, 2, 0 }, /* RDR 25 */
  102. { 11, 1, 0 }, /* RDR 26 */
  103. { 18, 1, 0 }, /* RDR 27 */
  104. { 128, 2, 0 }, /* RDR 28 */
  105. { 0, 0, 0 }, /* RDR 29 */
  106. { 16, 1, 0 }, /* RDR 30 */
  107. { 16, 1, 0 }, /* RDR 31 */
  108. };
  109. /* RDR register descriptions for PCX-U */
  110. static const struct rdr_tbl_ent perf_rdr_tbl_U[] = {
  111. { 19, 1, 8 }, /* RDR 0 */
  112. { 32, 1, 16 }, /* RDR 1 */
  113. { 20, 1, 0 }, /* RDR 2 */
  114. { 0, 0, 0 }, /* RDR 3 */
  115. { 344, 6, 0 }, /* RDR 4 */
  116. { 176, 3, 0 }, /* RDR 5 */
  117. { 336, 6, 0 }, /* RDR 6 */
  118. { 0, 0, 0 }, /* RDR 7 */
  119. { 0, 0, 0 }, /* RDR 8 */
  120. { 0, 0, 0 }, /* RDR 9 */
  121. { 28, 1, 0 }, /* RDR 10 */
  122. { 33, 1, 0 }, /* RDR 11 */
  123. { 0, 0, 0 }, /* RDR 12 */
  124. { 230, 4, 0 }, /* RDR 13 */
  125. { 32, 1, 0 }, /* RDR 14 */
  126. { 128, 2, 0 }, /* RDR 15 */
  127. { 1494, 24, 0 }, /* RDR 16 */
  128. { 18, 1, 0 }, /* RDR 17 */
  129. { 4, 1, 0 }, /* RDR 18 */
  130. { 0, 0, 0 }, /* RDR 19 */
  131. { 158, 3, 24 }, /* RDR 20 */
  132. { 158, 3, 24 }, /* RDR 21 */
  133. { 194, 4, 48 }, /* RDR 22 */
  134. { 194, 4, 48 }, /* RDR 23 */
  135. { 71, 2, 0 }, /* RDR 24 */
  136. { 71, 2, 0 }, /* RDR 25 */
  137. { 28, 1, 0 }, /* RDR 26 */
  138. { 33, 1, 0 }, /* RDR 27 */
  139. { 88, 2, 0 }, /* RDR 28 */
  140. { 32, 1, 0 }, /* RDR 29 */
  141. { 24, 1, 0 }, /* RDR 30 */
  142. { 16, 1, 0 }, /* RDR 31 */
  143. };
  144. /*
  145. * A non-zero write_control in the above tables is a byte offset into
  146. * this array.
  147. */
  148. static const uint64_t perf_bitmasks[] = {
  149. 0x0000000000000000ul, /* first dbl word must be zero */
  150. 0xfdffe00000000000ul, /* RDR0 bitmask */
  151. 0x003f000000000000ul, /* RDR1 bitmask */
  152. 0x00fffffffffffffful, /* RDR20-RDR21 bitmask (152 bits) */
  153. 0xfffffffffffffffful,
  154. 0xfffffffc00000000ul,
  155. 0xfffffffffffffffful, /* RDR22-RDR23 bitmask (233 bits) */
  156. 0xfffffffffffffffful,
  157. 0xfffffffffffffffcul,
  158. 0xff00000000000000ul
  159. };
  160. /*
  161. * Write control bitmasks for Pa-8700 processor given
  162. * some things have changed slightly.
  163. */
  164. static const uint64_t perf_bitmasks_piranha[] = {
  165. 0x0000000000000000ul, /* first dbl word must be zero */
  166. 0xfdffe00000000000ul, /* RDR0 bitmask */
  167. 0x003f000000000000ul, /* RDR1 bitmask */
  168. 0x00fffffffffffffful, /* RDR20-RDR21 bitmask (158 bits) */
  169. 0xfffffffffffffffful,
  170. 0xfffffffc00000000ul,
  171. 0xfffffffffffffffful, /* RDR22-RDR23 bitmask (210 bits) */
  172. 0xfffffffffffffffful,
  173. 0xfffffffffffffffful,
  174. 0xfffc000000000000ul
  175. };
  176. static const uint64_t *bitmask_array; /* array of bitmasks to use */
  177. /******************************************************************************
  178. * Function Prototypes
  179. *****************************************************************************/
  180. static int perf_config(uint32_t *image_ptr);
  181. static int perf_release(struct inode *inode, struct file *file);
  182. static int perf_open(struct inode *inode, struct file *file);
  183. static ssize_t perf_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos);
  184. static ssize_t perf_write(struct file *file, const char __user *buf, size_t count,
  185. loff_t *ppos);
  186. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  187. static void perf_start_counters(void);
  188. static int perf_stop_counters(uint32_t *raddr);
  189. static const struct rdr_tbl_ent * perf_rdr_get_entry(uint32_t rdr_num);
  190. static int perf_rdr_read_ubuf(uint32_t rdr_num, uint64_t *buffer);
  191. static int perf_rdr_clear(uint32_t rdr_num);
  192. static int perf_write_image(uint64_t *memaddr);
  193. static void perf_rdr_write(uint32_t rdr_num, uint64_t *buffer);
  194. /* External Assembly Routines */
  195. extern uint64_t perf_rdr_shift_in_W (uint32_t rdr_num, uint16_t width);
  196. extern uint64_t perf_rdr_shift_in_U (uint32_t rdr_num, uint16_t width);
  197. extern void perf_rdr_shift_out_W (uint32_t rdr_num, uint64_t buffer);
  198. extern void perf_rdr_shift_out_U (uint32_t rdr_num, uint64_t buffer);
  199. extern void perf_intrigue_enable_perf_counters (void);
  200. extern void perf_intrigue_disable_perf_counters (void);
  201. /******************************************************************************
  202. * Function Definitions
  203. *****************************************************************************/
  204. /*
  205. * configure:
  206. *
  207. * Configure the cpu with a given data image. First turn off the counters,
  208. * then download the image, then turn the counters back on.
  209. */
  210. static int perf_config(uint32_t *image_ptr)
  211. {
  212. long error;
  213. uint32_t raddr[4];
  214. /* Stop the counters*/
  215. error = perf_stop_counters(raddr);
  216. if (error != 0) {
  217. printk("perf_config: perf_stop_counters = %ld\n", error);
  218. return -EINVAL;
  219. }
  220. printk("Preparing to write image\n");
  221. /* Write the image to the chip */
  222. error = perf_write_image((uint64_t *)image_ptr);
  223. if (error != 0) {
  224. printk("perf_config: DOWNLOAD = %ld\n", error);
  225. return -EINVAL;
  226. }
  227. printk("Preparing to start counters\n");
  228. /* Start the counters */
  229. perf_start_counters();
  230. return sizeof(uint32_t);
  231. }
  232. /*
  233. * Open the device and initialize all of its memory. The device is only
  234. * opened once, but can be "queried" by multiple processes that know its
  235. * file descriptor.
  236. */
  237. static int perf_open(struct inode *inode, struct file *file)
  238. {
  239. lock_kernel();
  240. spin_lock(&perf_lock);
  241. if (perf_enabled) {
  242. spin_unlock(&perf_lock);
  243. unlock_kernel();
  244. return -EBUSY;
  245. }
  246. perf_enabled = 1;
  247. spin_unlock(&perf_lock);
  248. unlock_kernel();
  249. return 0;
  250. }
  251. /*
  252. * Close the device.
  253. */
  254. static int perf_release(struct inode *inode, struct file *file)
  255. {
  256. spin_lock(&perf_lock);
  257. perf_enabled = 0;
  258. spin_unlock(&perf_lock);
  259. return 0;
  260. }
  261. /*
  262. * Read does nothing for this driver
  263. */
  264. static ssize_t perf_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
  265. {
  266. return 0;
  267. }
  268. /*
  269. * write:
  270. *
  271. * This routine downloads the image to the chip. It must be
  272. * called on the processor that the download should happen
  273. * on.
  274. */
  275. static ssize_t perf_write(struct file *file, const char __user *buf, size_t count,
  276. loff_t *ppos)
  277. {
  278. int err;
  279. size_t image_size;
  280. uint32_t image_type;
  281. uint32_t interface_type;
  282. uint32_t test;
  283. if (perf_processor_interface == ONYX_INTF)
  284. image_size = PCXU_IMAGE_SIZE;
  285. else if (perf_processor_interface == CUDA_INTF)
  286. image_size = PCXW_IMAGE_SIZE;
  287. else
  288. return -EFAULT;
  289. if (!capable(CAP_SYS_ADMIN))
  290. return -EACCES;
  291. if (count != sizeof(uint32_t))
  292. return -EIO;
  293. if ((err = copy_from_user(&image_type, buf, sizeof(uint32_t))) != 0)
  294. return err;
  295. /* Get the interface type and test type */
  296. interface_type = (image_type >> 16) & 0xffff;
  297. test = (image_type & 0xffff);
  298. /* Make sure everything makes sense */
  299. /* First check the machine type is correct for
  300. the requested image */
  301. if (((perf_processor_interface == CUDA_INTF) &&
  302. (interface_type != CUDA_INTF)) ||
  303. ((perf_processor_interface == ONYX_INTF) &&
  304. (interface_type != ONYX_INTF)))
  305. return -EINVAL;
  306. /* Next check to make sure the requested image
  307. is valid */
  308. if (((interface_type == CUDA_INTF) &&
  309. (test >= MAX_CUDA_IMAGES)) ||
  310. ((interface_type == ONYX_INTF) &&
  311. (test >= MAX_ONYX_IMAGES)))
  312. return -EINVAL;
  313. /* Copy the image into the processor */
  314. if (interface_type == CUDA_INTF)
  315. return perf_config(cuda_images[test]);
  316. else
  317. return perf_config(onyx_images[test]);
  318. return count;
  319. }
  320. /*
  321. * Patch the images that need to know the IVA addresses.
  322. */
  323. static void perf_patch_images(void)
  324. {
  325. #if 0 /* FIXME!! */
  326. /*
  327. * NOTE: this routine is VERY specific to the current TLB image.
  328. * If the image is changed, this routine might also need to be changed.
  329. */
  330. extern void $i_itlb_miss_2_0();
  331. extern void $i_dtlb_miss_2_0();
  332. extern void PA2_0_iva();
  333. /*
  334. * We can only use the lower 32-bits, the upper 32-bits should be 0
  335. * anyway given this is in the kernel
  336. */
  337. uint32_t itlb_addr = (uint32_t)&($i_itlb_miss_2_0);
  338. uint32_t dtlb_addr = (uint32_t)&($i_dtlb_miss_2_0);
  339. uint32_t IVAaddress = (uint32_t)&PA2_0_iva;
  340. if (perf_processor_interface == ONYX_INTF) {
  341. /* clear last 2 bytes */
  342. onyx_images[TLBMISS][15] &= 0xffffff00;
  343. /* set 2 bytes */
  344. onyx_images[TLBMISS][15] |= (0x000000ff&((dtlb_addr) >> 24));
  345. onyx_images[TLBMISS][16] = (dtlb_addr << 8)&0xffffff00;
  346. onyx_images[TLBMISS][17] = itlb_addr;
  347. /* clear last 2 bytes */
  348. onyx_images[TLBHANDMISS][15] &= 0xffffff00;
  349. /* set 2 bytes */
  350. onyx_images[TLBHANDMISS][15] |= (0x000000ff&((dtlb_addr) >> 24));
  351. onyx_images[TLBHANDMISS][16] = (dtlb_addr << 8)&0xffffff00;
  352. onyx_images[TLBHANDMISS][17] = itlb_addr;
  353. /* clear last 2 bytes */
  354. onyx_images[BIG_CPI][15] &= 0xffffff00;
  355. /* set 2 bytes */
  356. onyx_images[BIG_CPI][15] |= (0x000000ff&((dtlb_addr) >> 24));
  357. onyx_images[BIG_CPI][16] = (dtlb_addr << 8)&0xffffff00;
  358. onyx_images[BIG_CPI][17] = itlb_addr;
  359. onyx_images[PANIC][15] &= 0xffffff00; /* clear last 2 bytes */
  360. onyx_images[PANIC][15] |= (0x000000ff&((IVAaddress) >> 24)); /* set 2 bytes */
  361. onyx_images[PANIC][16] = (IVAaddress << 8)&0xffffff00;
  362. } else if (perf_processor_interface == CUDA_INTF) {
  363. /* Cuda interface */
  364. cuda_images[TLBMISS][16] =
  365. (cuda_images[TLBMISS][16]&0xffff0000) |
  366. ((dtlb_addr >> 8)&0x0000ffff);
  367. cuda_images[TLBMISS][17] =
  368. ((dtlb_addr << 24)&0xff000000) | ((itlb_addr >> 16)&0x000000ff);
  369. cuda_images[TLBMISS][18] = (itlb_addr << 16)&0xffff0000;
  370. cuda_images[TLBHANDMISS][16] =
  371. (cuda_images[TLBHANDMISS][16]&0xffff0000) |
  372. ((dtlb_addr >> 8)&0x0000ffff);
  373. cuda_images[TLBHANDMISS][17] =
  374. ((dtlb_addr << 24)&0xff000000) | ((itlb_addr >> 16)&0x000000ff);
  375. cuda_images[TLBHANDMISS][18] = (itlb_addr << 16)&0xffff0000;
  376. cuda_images[BIG_CPI][16] =
  377. (cuda_images[BIG_CPI][16]&0xffff0000) |
  378. ((dtlb_addr >> 8)&0x0000ffff);
  379. cuda_images[BIG_CPI][17] =
  380. ((dtlb_addr << 24)&0xff000000) | ((itlb_addr >> 16)&0x000000ff);
  381. cuda_images[BIG_CPI][18] = (itlb_addr << 16)&0xffff0000;
  382. } else {
  383. /* Unknown type */
  384. }
  385. #endif
  386. }
  387. /*
  388. * ioctl routine
  389. * All routines effect the processor that they are executed on. Thus you
  390. * must be running on the processor that you wish to change.
  391. */
  392. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  393. {
  394. long error_start;
  395. uint32_t raddr[4];
  396. int error = 0;
  397. switch (cmd) {
  398. case PA_PERF_ON:
  399. /* Start the counters */
  400. perf_start_counters();
  401. break;
  402. case PA_PERF_OFF:
  403. error_start = perf_stop_counters(raddr);
  404. if (error_start != 0) {
  405. printk(KERN_ERR "perf_off: perf_stop_counters = %ld\n", error_start);
  406. error = -EFAULT;
  407. break;
  408. }
  409. /* copy out the Counters */
  410. if (copy_to_user((void __user *)arg, raddr,
  411. sizeof (raddr)) != 0) {
  412. error = -EFAULT;
  413. break;
  414. }
  415. break;
  416. case PA_PERF_VERSION:
  417. /* Return the version # */
  418. error = put_user(PERF_VERSION, (int *)arg);
  419. break;
  420. default:
  421. error = -ENOTTY;
  422. }
  423. return error;
  424. }
  425. static const struct file_operations perf_fops = {
  426. .llseek = no_llseek,
  427. .read = perf_read,
  428. .write = perf_write,
  429. .unlocked_ioctl = perf_ioctl,
  430. .compat_ioctl = perf_ioctl,
  431. .open = perf_open,
  432. .release = perf_release
  433. };
  434. static struct miscdevice perf_dev = {
  435. MISC_DYNAMIC_MINOR,
  436. PA_PERF_DEV,
  437. &perf_fops
  438. };
  439. /*
  440. * Initialize the module
  441. */
  442. static int __init perf_init(void)
  443. {
  444. int ret;
  445. /* Determine correct processor interface to use */
  446. bitmask_array = perf_bitmasks;
  447. if (boot_cpu_data.cpu_type == pcxu ||
  448. boot_cpu_data.cpu_type == pcxu_) {
  449. perf_processor_interface = ONYX_INTF;
  450. } else if (boot_cpu_data.cpu_type == pcxw ||
  451. boot_cpu_data.cpu_type == pcxw_ ||
  452. boot_cpu_data.cpu_type == pcxw2 ||
  453. boot_cpu_data.cpu_type == mako ||
  454. boot_cpu_data.cpu_type == mako2) {
  455. perf_processor_interface = CUDA_INTF;
  456. if (boot_cpu_data.cpu_type == pcxw2 ||
  457. boot_cpu_data.cpu_type == mako ||
  458. boot_cpu_data.cpu_type == mako2)
  459. bitmask_array = perf_bitmasks_piranha;
  460. } else {
  461. perf_processor_interface = UNKNOWN_INTF;
  462. printk("Performance monitoring counters not supported on this processor\n");
  463. return -ENODEV;
  464. }
  465. ret = misc_register(&perf_dev);
  466. if (ret) {
  467. printk(KERN_ERR "Performance monitoring counters: "
  468. "cannot register misc device.\n");
  469. return ret;
  470. }
  471. /* Patch the images to match the system */
  472. perf_patch_images();
  473. spin_lock_init(&perf_lock);
  474. /* TODO: this only lets us access the first cpu.. what to do for SMP? */
  475. cpu_device = per_cpu(cpu_data, 0).dev;
  476. printk("Performance monitoring counters enabled for %s\n",
  477. per_cpu(cpu_data, 0).dev->name);
  478. return 0;
  479. }
  480. /*
  481. * perf_start_counters(void)
  482. *
  483. * Start the counters.
  484. */
  485. static void perf_start_counters(void)
  486. {
  487. /* Enable performance monitor counters */
  488. perf_intrigue_enable_perf_counters();
  489. }
  490. /*
  491. * perf_stop_counters
  492. *
  493. * Stop the performance counters and save counts
  494. * in a per_processor array.
  495. */
  496. static int perf_stop_counters(uint32_t *raddr)
  497. {
  498. uint64_t userbuf[MAX_RDR_WORDS];
  499. /* Disable performance counters */
  500. perf_intrigue_disable_perf_counters();
  501. if (perf_processor_interface == ONYX_INTF) {
  502. uint64_t tmp64;
  503. /*
  504. * Read the counters
  505. */
  506. if (!perf_rdr_read_ubuf(16, userbuf))
  507. return -13;
  508. /* Counter0 is bits 1398 to 1429 */
  509. tmp64 = (userbuf[21] << 22) & 0x00000000ffc00000;
  510. tmp64 |= (userbuf[22] >> 42) & 0x00000000003fffff;
  511. /* OR sticky0 (bit 1430) to counter0 bit 32 */
  512. tmp64 |= (userbuf[22] >> 10) & 0x0000000080000000;
  513. raddr[0] = (uint32_t)tmp64;
  514. /* Counter1 is bits 1431 to 1462 */
  515. tmp64 = (userbuf[22] >> 9) & 0x00000000ffffffff;
  516. /* OR sticky1 (bit 1463) to counter1 bit 32 */
  517. tmp64 |= (userbuf[22] << 23) & 0x0000000080000000;
  518. raddr[1] = (uint32_t)tmp64;
  519. /* Counter2 is bits 1464 to 1495 */
  520. tmp64 = (userbuf[22] << 24) & 0x00000000ff000000;
  521. tmp64 |= (userbuf[23] >> 40) & 0x0000000000ffffff;
  522. /* OR sticky2 (bit 1496) to counter2 bit 32 */
  523. tmp64 |= (userbuf[23] >> 8) & 0x0000000080000000;
  524. raddr[2] = (uint32_t)tmp64;
  525. /* Counter3 is bits 1497 to 1528 */
  526. tmp64 = (userbuf[23] >> 7) & 0x00000000ffffffff;
  527. /* OR sticky3 (bit 1529) to counter3 bit 32 */
  528. tmp64 |= (userbuf[23] << 25) & 0x0000000080000000;
  529. raddr[3] = (uint32_t)tmp64;
  530. /*
  531. * Zero out the counters
  532. */
  533. /*
  534. * The counters and sticky-bits comprise the last 132 bits
  535. * (1398 - 1529) of RDR16 on a U chip. We'll zero these
  536. * out the easy way: zero out last 10 bits of dword 21,
  537. * all of dword 22 and 58 bits (plus 6 don't care bits) of
  538. * dword 23.
  539. */
  540. userbuf[21] &= 0xfffffffffffffc00ul; /* 0 to last 10 bits */
  541. userbuf[22] = 0;
  542. userbuf[23] = 0;
  543. /*
  544. * Write back the zeroed bytes + the image given
  545. * the read was destructive.
  546. */
  547. perf_rdr_write(16, userbuf);
  548. } else {
  549. /*
  550. * Read RDR-15 which contains the counters and sticky bits
  551. */
  552. if (!perf_rdr_read_ubuf(15, userbuf)) {
  553. return -13;
  554. }
  555. /*
  556. * Clear out the counters
  557. */
  558. perf_rdr_clear(15);
  559. /*
  560. * Copy the counters
  561. */
  562. raddr[0] = (uint32_t)((userbuf[0] >> 32) & 0x00000000ffffffffUL);
  563. raddr[1] = (uint32_t)(userbuf[0] & 0x00000000ffffffffUL);
  564. raddr[2] = (uint32_t)((userbuf[1] >> 32) & 0x00000000ffffffffUL);
  565. raddr[3] = (uint32_t)(userbuf[1] & 0x00000000ffffffffUL);
  566. }
  567. return 0;
  568. }
  569. /*
  570. * perf_rdr_get_entry
  571. *
  572. * Retrieve a pointer to the description of what this
  573. * RDR contains.
  574. */
  575. static const struct rdr_tbl_ent * perf_rdr_get_entry(uint32_t rdr_num)
  576. {
  577. if (perf_processor_interface == ONYX_INTF) {
  578. return &perf_rdr_tbl_U[rdr_num];
  579. } else {
  580. return &perf_rdr_tbl_W[rdr_num];
  581. }
  582. }
  583. /*
  584. * perf_rdr_read_ubuf
  585. *
  586. * Read the RDR value into the buffer specified.
  587. */
  588. static int perf_rdr_read_ubuf(uint32_t rdr_num, uint64_t *buffer)
  589. {
  590. uint64_t data, data_mask = 0;
  591. uint32_t width, xbits, i;
  592. const struct rdr_tbl_ent *tentry;
  593. tentry = perf_rdr_get_entry(rdr_num);
  594. if ((width = tentry->width) == 0)
  595. return 0;
  596. /* Clear out buffer */
  597. i = tentry->num_words;
  598. while (i--) {
  599. buffer[i] = 0;
  600. }
  601. /* Check for bits an even number of 64 */
  602. if ((xbits = width & 0x03f) != 0) {
  603. data_mask = 1;
  604. data_mask <<= (64 - xbits);
  605. data_mask--;
  606. }
  607. /* Grab all of the data */
  608. i = tentry->num_words;
  609. while (i--) {
  610. if (perf_processor_interface == ONYX_INTF) {
  611. data = perf_rdr_shift_in_U(rdr_num, width);
  612. } else {
  613. data = perf_rdr_shift_in_W(rdr_num, width);
  614. }
  615. if (xbits) {
  616. buffer[i] |= (data << (64 - xbits));
  617. if (i) {
  618. buffer[i-1] |= ((data >> xbits) & data_mask);
  619. }
  620. } else {
  621. buffer[i] = data;
  622. }
  623. }
  624. return 1;
  625. }
  626. /*
  627. * perf_rdr_clear
  628. *
  629. * Zero out the given RDR register
  630. */
  631. static int perf_rdr_clear(uint32_t rdr_num)
  632. {
  633. const struct rdr_tbl_ent *tentry;
  634. int32_t i;
  635. tentry = perf_rdr_get_entry(rdr_num);
  636. if (tentry->width == 0) {
  637. return -1;
  638. }
  639. i = tentry->num_words;
  640. while (i--) {
  641. if (perf_processor_interface == ONYX_INTF) {
  642. perf_rdr_shift_out_U(rdr_num, 0UL);
  643. } else {
  644. perf_rdr_shift_out_W(rdr_num, 0UL);
  645. }
  646. }
  647. return 0;
  648. }
  649. /*
  650. * perf_write_image
  651. *
  652. * Write the given image out to the processor
  653. */
  654. static int perf_write_image(uint64_t *memaddr)
  655. {
  656. uint64_t buffer[MAX_RDR_WORDS];
  657. uint64_t *bptr;
  658. uint32_t dwords;
  659. const uint32_t *intrigue_rdr;
  660. const uint64_t *intrigue_bitmask;
  661. uint64_t tmp64;
  662. void __iomem *runway;
  663. const struct rdr_tbl_ent *tentry;
  664. int i;
  665. /* Clear out counters */
  666. if (perf_processor_interface == ONYX_INTF) {
  667. perf_rdr_clear(16);
  668. /* Toggle performance monitor */
  669. perf_intrigue_enable_perf_counters();
  670. perf_intrigue_disable_perf_counters();
  671. intrigue_rdr = perf_rdrs_U;
  672. } else {
  673. perf_rdr_clear(15);
  674. intrigue_rdr = perf_rdrs_W;
  675. }
  676. /* Write all RDRs */
  677. while (*intrigue_rdr != -1) {
  678. tentry = perf_rdr_get_entry(*intrigue_rdr);
  679. perf_rdr_read_ubuf(*intrigue_rdr, buffer);
  680. bptr = &buffer[0];
  681. dwords = tentry->num_words;
  682. if (tentry->write_control) {
  683. intrigue_bitmask = &bitmask_array[tentry->write_control >> 3];
  684. while (dwords--) {
  685. tmp64 = *intrigue_bitmask & *memaddr++;
  686. tmp64 |= (~(*intrigue_bitmask++)) & *bptr;
  687. *bptr++ = tmp64;
  688. }
  689. } else {
  690. while (dwords--) {
  691. *bptr++ = *memaddr++;
  692. }
  693. }
  694. perf_rdr_write(*intrigue_rdr, buffer);
  695. intrigue_rdr++;
  696. }
  697. /*
  698. * Now copy out the Runway stuff which is not in RDRs
  699. */
  700. if (cpu_device == NULL)
  701. {
  702. printk(KERN_ERR "write_image: cpu_device not yet initialized!\n");
  703. return -1;
  704. }
  705. runway = ioremap_nocache(cpu_device->hpa.start, 4096);
  706. /* Merge intrigue bits into Runway STATUS 0 */
  707. tmp64 = __raw_readq(runway + RUNWAY_STATUS) & 0xffecfffffffffffful;
  708. __raw_writeq(tmp64 | (*memaddr++ & 0x0013000000000000ul),
  709. runway + RUNWAY_STATUS);
  710. /* Write RUNWAY DEBUG registers */
  711. for (i = 0; i < 8; i++) {
  712. __raw_writeq(*memaddr++, runway + RUNWAY_DEBUG);
  713. }
  714. return 0;
  715. }
  716. /*
  717. * perf_rdr_write
  718. *
  719. * Write the given RDR register with the contents
  720. * of the given buffer.
  721. */
  722. static void perf_rdr_write(uint32_t rdr_num, uint64_t *buffer)
  723. {
  724. const struct rdr_tbl_ent *tentry;
  725. int32_t i;
  726. printk("perf_rdr_write\n");
  727. tentry = perf_rdr_get_entry(rdr_num);
  728. if (tentry->width == 0) { return; }
  729. i = tentry->num_words;
  730. while (i--) {
  731. if (perf_processor_interface == ONYX_INTF) {
  732. perf_rdr_shift_out_U(rdr_num, buffer[i]);
  733. } else {
  734. perf_rdr_shift_out_W(rdr_num, buffer[i]);
  735. }
  736. }
  737. printk("perf_rdr_write done\n");
  738. }
  739. module_init(perf_init);