perf.c 22 KB

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