OSDN Git Service

0971ebbde1b7fbc70116165725be1e8c12c9bd35
[uclinux-h8/linux.git] / arch / tile / include / hv / hypervisor.h
1 /*
2  * Copyright 2010 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful, but
9  *   WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  */
14
15 /**
16  * @file hypervisor.h
17  * The hypervisor's public API.
18  */
19
20 #ifndef _HV_HV_H
21 #define _HV_HV_H
22
23 #include <arch/chip.h>
24
25 /* Linux builds want unsigned long constants, but assembler wants numbers */
26 #ifdef __ASSEMBLER__
27 /** One, for assembler */
28 #define __HV_SIZE_ONE 1
29 #elif !defined(__tile__) && CHIP_VA_WIDTH() > 32
30 /** One, for 64-bit on host */
31 #define __HV_SIZE_ONE 1ULL
32 #else
33 /** One, for Linux */
34 #define __HV_SIZE_ONE 1UL
35 #endif
36
37 /** The log2 of the span of a level-1 page table, in bytes.
38  */
39 #define HV_LOG2_L1_SPAN 32
40
41 /** The span of a level-1 page table, in bytes.
42  */
43 #define HV_L1_SPAN (__HV_SIZE_ONE << HV_LOG2_L1_SPAN)
44
45 /** The log2 of the initial size of small pages, in bytes.
46  * See HV_DEFAULT_PAGE_SIZE_SMALL.
47  */
48 #define HV_LOG2_DEFAULT_PAGE_SIZE_SMALL 16
49
50 /** The initial size of small pages, in bytes. This value should be verified
51  * at runtime by calling hv_sysconf(HV_SYSCONF_PAGE_SIZE_SMALL).
52  * It may also be modified when installing a new context.
53  */
54 #define HV_DEFAULT_PAGE_SIZE_SMALL \
55   (__HV_SIZE_ONE << HV_LOG2_DEFAULT_PAGE_SIZE_SMALL)
56
57 /** The log2 of the initial size of large pages, in bytes.
58  * See HV_DEFAULT_PAGE_SIZE_LARGE.
59  */
60 #define HV_LOG2_DEFAULT_PAGE_SIZE_LARGE 24
61
62 /** The initial size of large pages, in bytes. This value should be verified
63  * at runtime by calling hv_sysconf(HV_SYSCONF_PAGE_SIZE_LARGE).
64  * It may also be modified when installing a new context.
65  */
66 #define HV_DEFAULT_PAGE_SIZE_LARGE \
67   (__HV_SIZE_ONE << HV_LOG2_DEFAULT_PAGE_SIZE_LARGE)
68
69 #if CHIP_VA_WIDTH() > 32
70
71 /** The log2 of the initial size of jumbo pages, in bytes.
72  * See HV_DEFAULT_PAGE_SIZE_JUMBO.
73  */
74 #define HV_LOG2_DEFAULT_PAGE_SIZE_JUMBO 32
75
76 /** The initial size of jumbo pages, in bytes. This value should
77  * be verified at runtime by calling hv_sysconf(HV_SYSCONF_PAGE_SIZE_JUMBO).
78  * It may also be modified when installing a new context.
79  */
80 #define HV_DEFAULT_PAGE_SIZE_JUMBO \
81   (__HV_SIZE_ONE << HV_LOG2_DEFAULT_PAGE_SIZE_JUMBO)
82
83 #endif
84
85 /** The log2 of the granularity at which page tables must be aligned;
86  *  in other words, the CPA for a page table must have this many zero
87  *  bits at the bottom of the address.
88  */
89 #define HV_LOG2_PAGE_TABLE_ALIGN 11
90
91 /** The granularity at which page tables must be aligned.
92  */
93 #define HV_PAGE_TABLE_ALIGN (__HV_SIZE_ONE << HV_LOG2_PAGE_TABLE_ALIGN)
94
95 /** Normal start of hypervisor glue in client physical memory. */
96 #define HV_GLUE_START_CPA 0x10000
97
98 /** This much space is reserved at HV_GLUE_START_CPA
99  * for the hypervisor glue. The client program must start at
100  * some address higher than this, and in particular the address of
101  * its text section should be equal to zero modulo HV_PAGE_SIZE_LARGE
102  * so that relative offsets to the HV glue are correct.
103  */
104 #define HV_GLUE_RESERVED_SIZE 0x10000
105
106 /** Each entry in the hv dispatch array takes this many bytes. */
107 #define HV_DISPATCH_ENTRY_SIZE 32
108
109 /** Version of the hypervisor interface defined by this file */
110 #define _HV_VERSION 13
111
112 /** Last version of the hypervisor interface with old hv_init() ABI.
113  *
114  * The change from version 12 to version 13 corresponds to launching
115  * the client by default at PL2 instead of PL1 (corresponding to the
116  * hv itself running at PL3 instead of PL2).  To make this explicit,
117  * the hv_init() API was also extended so the client can report its
118  * desired PL, resulting in a more helpful failure diagnostic.  If you
119  * call hv_init() with _HV_VERSION_OLD_HV_INIT and omit the client_pl
120  * argument, the hypervisor will assume client_pl = 1.
121  *
122  * Note that this is a deprecated solution and we do not expect to
123  * support clients of the Tilera hypervisor running at PL1 indefinitely.
124  */
125 #define _HV_VERSION_OLD_HV_INIT 12
126
127 /* Index into hypervisor interface dispatch code blocks.
128  *
129  * Hypervisor calls are invoked from user space by calling code
130  * at an address HV_BASE_ADDRESS + (index) * HV_DISPATCH_ENTRY_SIZE,
131  * where index is one of these enum values.
132  *
133  * Normally a supervisor is expected to produce a set of symbols
134  * starting at HV_BASE_ADDRESS that obey this convention, but a user
135  * program could call directly through function pointers if desired.
136  *
137  * These numbers are part of the binary API and will not be changed
138  * without updating HV_VERSION, which should be a rare event.
139  */
140
141 /** reserved. */
142 #define _HV_DISPATCH_RESERVED                     0
143
144 /** hv_init  */
145 #define HV_DISPATCH_INIT                          1
146
147 /** hv_install_context */
148 #define HV_DISPATCH_INSTALL_CONTEXT               2
149
150 /** hv_sysconf */
151 #define HV_DISPATCH_SYSCONF                       3
152
153 /** hv_get_rtc */
154 #define HV_DISPATCH_GET_RTC                       4
155
156 /** hv_set_rtc */
157 #define HV_DISPATCH_SET_RTC                       5
158
159 /** hv_flush_asid */
160 #define HV_DISPATCH_FLUSH_ASID                    6
161
162 /** hv_flush_page */
163 #define HV_DISPATCH_FLUSH_PAGE                    7
164
165 /** hv_flush_pages */
166 #define HV_DISPATCH_FLUSH_PAGES                   8
167
168 /** hv_restart */
169 #define HV_DISPATCH_RESTART                       9
170
171 /** hv_halt */
172 #define HV_DISPATCH_HALT                          10
173
174 /** hv_power_off */
175 #define HV_DISPATCH_POWER_OFF                     11
176
177 /** hv_inquire_physical */
178 #define HV_DISPATCH_INQUIRE_PHYSICAL              12
179
180 /** hv_inquire_memory_controller */
181 #define HV_DISPATCH_INQUIRE_MEMORY_CONTROLLER     13
182
183 /** hv_inquire_virtual */
184 #define HV_DISPATCH_INQUIRE_VIRTUAL               14
185
186 /** hv_inquire_asid */
187 #define HV_DISPATCH_INQUIRE_ASID                  15
188
189 /** hv_nanosleep */
190 #define HV_DISPATCH_NANOSLEEP                     16
191
192 /** hv_console_read_if_ready */
193 #define HV_DISPATCH_CONSOLE_READ_IF_READY         17
194
195 /** hv_console_write */
196 #define HV_DISPATCH_CONSOLE_WRITE                 18
197
198 /** hv_downcall_dispatch */
199 #define HV_DISPATCH_DOWNCALL_DISPATCH             19
200
201 /** hv_inquire_topology */
202 #define HV_DISPATCH_INQUIRE_TOPOLOGY              20
203
204 /** hv_fs_findfile */
205 #define HV_DISPATCH_FS_FINDFILE                   21
206
207 /** hv_fs_fstat */
208 #define HV_DISPATCH_FS_FSTAT                      22
209
210 /** hv_fs_pread */
211 #define HV_DISPATCH_FS_PREAD                      23
212
213 /** hv_physaddr_read64 */
214 #define HV_DISPATCH_PHYSADDR_READ64               24
215
216 /** hv_physaddr_write64 */
217 #define HV_DISPATCH_PHYSADDR_WRITE64              25
218
219 /** hv_get_command_line */
220 #define HV_DISPATCH_GET_COMMAND_LINE              26
221
222 /** hv_set_caching */
223 #define HV_DISPATCH_SET_CACHING                   27
224
225 /** hv_bzero_page */
226 #define HV_DISPATCH_BZERO_PAGE                    28
227
228 /** hv_register_message_state */
229 #define HV_DISPATCH_REGISTER_MESSAGE_STATE        29
230
231 /** hv_send_message */
232 #define HV_DISPATCH_SEND_MESSAGE                  30
233
234 /** hv_receive_message */
235 #define HV_DISPATCH_RECEIVE_MESSAGE               31
236
237 /** hv_inquire_context */
238 #define HV_DISPATCH_INQUIRE_CONTEXT               32
239
240 /** hv_start_all_tiles */
241 #define HV_DISPATCH_START_ALL_TILES               33
242
243 /** hv_dev_open */
244 #define HV_DISPATCH_DEV_OPEN                      34
245
246 /** hv_dev_close */
247 #define HV_DISPATCH_DEV_CLOSE                     35
248
249 /** hv_dev_pread */
250 #define HV_DISPATCH_DEV_PREAD                     36
251
252 /** hv_dev_pwrite */
253 #define HV_DISPATCH_DEV_PWRITE                    37
254
255 /** hv_dev_poll */
256 #define HV_DISPATCH_DEV_POLL                      38
257
258 /** hv_dev_poll_cancel */
259 #define HV_DISPATCH_DEV_POLL_CANCEL               39
260
261 /** hv_dev_preada */
262 #define HV_DISPATCH_DEV_PREADA                    40
263
264 /** hv_dev_pwritea */
265 #define HV_DISPATCH_DEV_PWRITEA                   41
266
267 /** hv_flush_remote */
268 #define HV_DISPATCH_FLUSH_REMOTE                  42
269
270 /** hv_console_putc */
271 #define HV_DISPATCH_CONSOLE_PUTC                  43
272
273 /** hv_inquire_tiles */
274 #define HV_DISPATCH_INQUIRE_TILES                 44
275
276 /** hv_confstr */
277 #define HV_DISPATCH_CONFSTR                       45
278
279 /** hv_reexec */
280 #define HV_DISPATCH_REEXEC                        46
281
282 /** hv_set_command_line */
283 #define HV_DISPATCH_SET_COMMAND_LINE              47
284
285 #if !CHIP_HAS_IPI()
286
287 /** hv_clear_intr */
288 #define HV_DISPATCH_CLEAR_INTR                    48
289
290 /** hv_enable_intr */
291 #define HV_DISPATCH_ENABLE_INTR                   49
292
293 /** hv_disable_intr */
294 #define HV_DISPATCH_DISABLE_INTR                  50
295
296 /** hv_raise_intr */
297 #define HV_DISPATCH_RAISE_INTR                    51
298
299 /** hv_trigger_ipi */
300 #define HV_DISPATCH_TRIGGER_IPI                   52
301
302 #endif /* !CHIP_HAS_IPI() */
303
304 /** hv_store_mapping */
305 #define HV_DISPATCH_STORE_MAPPING                 53
306
307 /** hv_inquire_realpa */
308 #define HV_DISPATCH_INQUIRE_REALPA                54
309
310 /** hv_flush_all */
311 #define HV_DISPATCH_FLUSH_ALL                     55
312
313 #if CHIP_HAS_IPI()
314 /** hv_get_ipi_pte */
315 #define HV_DISPATCH_GET_IPI_PTE                   56
316 #endif
317
318 /** hv_set_pte_super_shift */
319 #define HV_DISPATCH_SET_PTE_SUPER_SHIFT           57
320
321 /** hv_console_set_ipi */
322 #define HV_DISPATCH_CONSOLE_SET_IPI               63
323
324 /** One more than the largest dispatch value */
325 #define _HV_DISPATCH_END                          64
326
327
328 #ifndef __ASSEMBLER__
329
330 #ifdef __KERNEL__
331 #include <asm/types.h>
332 typedef u32 __hv32;        /**< 32-bit value */
333 typedef u64 __hv64;        /**< 64-bit value */
334 #else
335 #include <stdint.h>
336 typedef uint32_t __hv32;   /**< 32-bit value */
337 typedef uint64_t __hv64;   /**< 64-bit value */
338 #endif
339
340
341 /** Hypervisor physical address. */
342 typedef __hv64 HV_PhysAddr;
343
344 #if CHIP_VA_WIDTH() > 32
345 /** Hypervisor virtual address. */
346 typedef __hv64 HV_VirtAddr;
347 #else
348 /** Hypervisor virtual address. */
349 typedef __hv32 HV_VirtAddr;
350 #endif /* CHIP_VA_WIDTH() > 32 */
351
352 /** Hypervisor ASID. */
353 typedef unsigned int HV_ASID;
354
355 /** Hypervisor tile location for a memory access
356  * ("location overridden target").
357  */
358 typedef unsigned int HV_LOTAR;
359
360 /** Hypervisor size of a page. */
361 typedef unsigned long HV_PageSize;
362
363 /** A page table entry.
364  */
365 typedef struct
366 {
367   __hv64 val;                /**< Value of PTE */
368 } HV_PTE;
369
370 /** Hypervisor error code. */
371 typedef int HV_Errno;
372
373 #endif /* !__ASSEMBLER__ */
374
375 #define HV_OK           0    /**< No error */
376 #define HV_EINVAL      -801  /**< Invalid argument */
377 #define HV_ENODEV      -802  /**< No such device */
378 #define HV_ENOENT      -803  /**< No such file or directory */
379 #define HV_EBADF       -804  /**< Bad file number */
380 #define HV_EFAULT      -805  /**< Bad address */
381 #define HV_ERECIP      -806  /**< Bad recipients */
382 #define HV_E2BIG       -807  /**< Message too big */
383 #define HV_ENOTSUP     -808  /**< Service not supported */
384 #define HV_EBUSY       -809  /**< Device busy */
385 #define HV_ENOSYS      -810  /**< Invalid syscall */
386 #define HV_EPERM       -811  /**< No permission */
387 #define HV_ENOTREADY   -812  /**< Device not ready */
388 #define HV_EIO         -813  /**< I/O error */
389 #define HV_ENOMEM      -814  /**< Out of memory */
390 #define HV_EAGAIN      -815  /**< Try again */
391
392 #define HV_ERR_MAX     -801  /**< Largest HV error code */
393 #define HV_ERR_MIN     -815  /**< Smallest HV error code */
394
395 #ifndef __ASSEMBLER__
396
397 /** Pass HV_VERSION to hv_init to request this version of the interface. */
398 typedef enum {
399   HV_VERSION = _HV_VERSION,
400   HV_VERSION_OLD_HV_INIT = _HV_VERSION_OLD_HV_INIT,
401
402 } HV_VersionNumber;
403
404 /** Initializes the hypervisor.
405  *
406  * @param interface_version_number The version of the hypervisor interface
407  * that this program expects, typically HV_VERSION.
408  * @param chip_num Architecture number of the chip the client was built for.
409  * @param chip_rev_num Revision number of the chip the client was built for.
410  * @param client_pl Privilege level the client is built for
411  *   (not required if interface_version_number == HV_VERSION_OLD_HV_INIT).
412  */
413 void hv_init(HV_VersionNumber interface_version_number,
414              int chip_num, int chip_rev_num, int client_pl);
415
416
417 /** Queries we can make for hv_sysconf().
418  *
419  * These numbers are part of the binary API and guaranteed not to change.
420  */
421 typedef enum {
422   /** An invalid value; do not use. */
423   _HV_SYSCONF_RESERVED       = 0,
424
425   /** The length of the glue section containing the hv_ procs, in bytes. */
426   HV_SYSCONF_GLUE_SIZE       = 1,
427
428   /** The size of small pages, in bytes. */
429   HV_SYSCONF_PAGE_SIZE_SMALL = 2,
430
431   /** The size of large pages, in bytes. */
432   HV_SYSCONF_PAGE_SIZE_LARGE = 3,
433
434   /** Processor clock speed, in hertz. */
435   HV_SYSCONF_CPU_SPEED       = 4,
436
437   /** Processor temperature, in degrees Kelvin.  The value
438    *  HV_SYSCONF_TEMP_KTOC may be subtracted from this to get degrees
439    *  Celsius.  If that Celsius value is HV_SYSCONF_OVERTEMP, this indicates
440    *  that the temperature has hit an upper limit and is no longer being
441    *  accurately tracked.
442    */
443   HV_SYSCONF_CPU_TEMP        = 5,
444
445   /** Board temperature, in degrees Kelvin.  The value
446    *  HV_SYSCONF_TEMP_KTOC may be subtracted from this to get degrees
447    *  Celsius.  If that Celsius value is HV_SYSCONF_OVERTEMP, this indicates
448    *  that the temperature has hit an upper limit and is no longer being
449    *  accurately tracked.
450    */
451   HV_SYSCONF_BOARD_TEMP      = 6,
452
453   /** Legal page size bitmask for hv_install_context().
454    * For example, if 16KB and 64KB small pages are supported,
455    * it would return "HV_CTX_PG_SM_16K | HV_CTX_PG_SM_64K".
456    */
457   HV_SYSCONF_VALID_PAGE_SIZES = 7,
458
459   /** The size of jumbo pages, in bytes.
460    * If no jumbo pages are available, zero will be returned.
461    */
462   HV_SYSCONF_PAGE_SIZE_JUMBO = 8,
463
464 } HV_SysconfQuery;
465
466 /** Offset to subtract from returned Kelvin temperature to get degrees
467     Celsius. */
468 #define HV_SYSCONF_TEMP_KTOC 273
469
470 /** Pseudo-temperature value indicating that the temperature has
471  *  pegged at its upper limit and is no longer accurate; note that this is
472  *  the value after subtracting HV_SYSCONF_TEMP_KTOC. */
473 #define HV_SYSCONF_OVERTEMP 999
474
475 /** Query a configuration value from the hypervisor.
476  * @param query Which value is requested (HV_SYSCONF_xxx).
477  * @return The requested value, or -1 the requested value is illegal or
478  *         unavailable.
479  */
480 long hv_sysconf(HV_SysconfQuery query);
481
482
483 /** Queries we can make for hv_confstr().
484  *
485  * These numbers are part of the binary API and guaranteed not to change.
486  */
487 typedef enum {
488   /** An invalid value; do not use. */
489   _HV_CONFSTR_RESERVED        = 0,
490
491   /** Board part number. */
492   HV_CONFSTR_BOARD_PART_NUM   = 1,
493
494   /** Board serial number. */
495   HV_CONFSTR_BOARD_SERIAL_NUM = 2,
496
497   /** Chip serial number. */
498   HV_CONFSTR_CHIP_SERIAL_NUM  = 3,
499
500   /** Board revision level. */
501   HV_CONFSTR_BOARD_REV        = 4,
502
503   /** Hypervisor software version. */
504   HV_CONFSTR_HV_SW_VER        = 5,
505
506   /** The name for this chip model. */
507   HV_CONFSTR_CHIP_MODEL       = 6,
508
509   /** Human-readable board description. */
510   HV_CONFSTR_BOARD_DESC       = 7,
511
512   /** Human-readable description of the hypervisor configuration. */
513   HV_CONFSTR_HV_CONFIG        = 8,
514
515   /** Human-readable version string for the boot image (for instance,
516    *  who built it and when, what configuration file was used). */
517   HV_CONFSTR_HV_CONFIG_VER    = 9,
518
519   /** Mezzanine part number. */
520   HV_CONFSTR_MEZZ_PART_NUM   = 10,
521
522   /** Mezzanine serial number. */
523   HV_CONFSTR_MEZZ_SERIAL_NUM = 11,
524
525   /** Mezzanine revision level. */
526   HV_CONFSTR_MEZZ_REV        = 12,
527
528   /** Human-readable mezzanine description. */
529   HV_CONFSTR_MEZZ_DESC       = 13,
530
531   /** Control path for the onboard network switch. */
532   HV_CONFSTR_SWITCH_CONTROL  = 14,
533
534   /** Chip revision level. */
535   HV_CONFSTR_CHIP_REV        = 15,
536
537   /** CPU module part number. */
538   HV_CONFSTR_CPUMOD_PART_NUM = 16,
539
540   /** CPU module serial number. */
541   HV_CONFSTR_CPUMOD_SERIAL_NUM = 17,
542
543   /** CPU module revision level. */
544   HV_CONFSTR_CPUMOD_REV      = 18,
545
546   /** Human-readable CPU module description. */
547   HV_CONFSTR_CPUMOD_DESC     = 19
548
549 } HV_ConfstrQuery;
550
551 /** Query a configuration string from the hypervisor.
552  *
553  * @param query Identifier for the specific string to be retrieved
554  *        (HV_CONFSTR_xxx).
555  * @param buf Buffer in which to place the string.
556  * @param len Length of the buffer.
557  * @return If query is valid, then the length of the corresponding string,
558  *        including the trailing null; if this is greater than len, the string
559  *        was truncated.  If query is invalid, HV_EINVAL.  If the specified
560  *        buffer is not writable by the client, HV_EFAULT.
561  */
562 int hv_confstr(HV_ConfstrQuery query, HV_VirtAddr buf, int len);
563
564 /** Tile coordinate */
565 typedef struct
566 {
567   /** X coordinate, relative to supervisor's top-left coordinate */
568   int x;
569
570   /** Y coordinate, relative to supervisor's top-left coordinate */
571   int y;
572 } HV_Coord;
573
574
575 #if CHIP_HAS_IPI()
576
577 /** Get the PTE for sending an IPI to a particular tile.
578  *
579  * @param tile Tile which will receive the IPI.
580  * @param pl Indicates which IPI registers: 0 = IPI_0, 1 = IPI_1.
581  * @param pte Filled with resulting PTE.
582  * @result Zero if no error, non-zero for invalid parameters.
583  */
584 int hv_get_ipi_pte(HV_Coord tile, int pl, HV_PTE* pte);
585
586 /** Configure the console interrupt.
587  *
588  * When the console client interrupt is enabled, the hypervisor will
589  * deliver the specified IPI to the client in the following situations:
590  *
591  * - The console has at least one character available for input.
592  *
593  * - The console can accept new characters for output, and the last call
594  *   to hv_console_write() did not write all of the characters requested
595  *   by the client.
596  *
597  * Note that in some system configurations, console interrupt will not
598  * be available; clients should be prepared for this routine to fail and
599  * to fall back to periodic console polling in that case.
600  *
601  * @param ipi Index of the IPI register which will receive the interrupt.
602  * @param event IPI event number for console interrupt. If less than 0,
603  *        disable the console IPI interrupt.
604  * @param coord Tile to be targeted for console interrupt.
605  * @return 0 on success, otherwise, HV_EINVAL if illegal parameter,
606  *         HV_ENOTSUP if console interrupt are not available.
607  */
608 int hv_console_set_ipi(int ipi, int event, HV_Coord coord);
609
610 #else /* !CHIP_HAS_IPI() */
611
612 /** A set of interrupts. */
613 typedef __hv32 HV_IntrMask;
614
615 /** The low interrupt numbers are reserved for use by the client in
616  *  delivering IPIs.  Any interrupt numbers higher than this value are
617  *  reserved for use by HV device drivers. */
618 #define HV_MAX_IPI_INTERRUPT 7
619
620 /** Enable a set of device interrupts.
621  *
622  * @param enab_mask Bitmap of interrupts to enable.
623  */
624 void hv_enable_intr(HV_IntrMask enab_mask);
625
626 /** Disable a set of device interrupts.
627  *
628  * @param disab_mask Bitmap of interrupts to disable.
629  */
630 void hv_disable_intr(HV_IntrMask disab_mask);
631
632 /** Clear a set of device interrupts.
633  *
634  * @param clear_mask Bitmap of interrupts to clear.
635  */
636 void hv_clear_intr(HV_IntrMask clear_mask);
637
638 /** Raise a set of device interrupts.
639  *
640  * @param raise_mask Bitmap of interrupts to raise.
641  */
642 void hv_raise_intr(HV_IntrMask raise_mask);
643
644 /** Trigger a one-shot interrupt on some tile
645  *
646  * @param tile Which tile to interrupt.
647  * @param interrupt Interrupt number to trigger; must be between 0 and
648  *        HV_MAX_IPI_INTERRUPT.
649  * @return HV_OK on success, or a hypervisor error code.
650  */
651 HV_Errno hv_trigger_ipi(HV_Coord tile, int interrupt);
652
653 #endif /* !CHIP_HAS_IPI() */
654
655 /** Store memory mapping in debug memory so that external debugger can read it.
656  * A maximum of 16 entries can be stored.
657  *
658  * @param va VA of memory that is mapped.
659  * @param len Length of mapped memory.
660  * @param pa PA of memory that is mapped.
661  * @return 0 on success, -1 if the maximum number of mappings is exceeded.
662  */
663 int hv_store_mapping(HV_VirtAddr va, unsigned int len, HV_PhysAddr pa);
664
665 /** Given a client PA and a length, return its real (HV) PA.
666  *
667  * @param cpa Client physical address.
668  * @param len Length of mapped memory.
669  * @return physical address, or -1 if cpa or len is not valid.
670  */
671 HV_PhysAddr hv_inquire_realpa(HV_PhysAddr cpa, unsigned int len);
672
673 /** RTC return flag for no RTC chip present.
674  */
675 #define HV_RTC_NO_CHIP     0x1
676
677 /** RTC return flag for low-voltage condition, indicating that battery had
678  * died and time read is unreliable.
679  */
680 #define HV_RTC_LOW_VOLTAGE 0x2
681
682 /** Date/Time of day */
683 typedef struct {
684 #if CHIP_WORD_SIZE() > 32
685   __hv64 tm_sec;   /**< Seconds, 0-59 */
686   __hv64 tm_min;   /**< Minutes, 0-59 */
687   __hv64 tm_hour;  /**< Hours, 0-23 */
688   __hv64 tm_mday;  /**< Day of month, 0-30 */
689   __hv64 tm_mon;   /**< Month, 0-11 */
690   __hv64 tm_year;  /**< Years since 1900, 0-199 */
691   __hv64 flags;    /**< Return flags, 0 if no error */
692 #else
693   __hv32 tm_sec;   /**< Seconds, 0-59 */
694   __hv32 tm_min;   /**< Minutes, 0-59 */
695   __hv32 tm_hour;  /**< Hours, 0-23 */
696   __hv32 tm_mday;  /**< Day of month, 0-30 */
697   __hv32 tm_mon;   /**< Month, 0-11 */
698   __hv32 tm_year;  /**< Years since 1900, 0-199 */
699   __hv32 flags;    /**< Return flags, 0 if no error */
700 #endif
701 } HV_RTCTime;
702
703 /** Read the current time-of-day clock.
704  * @return HV_RTCTime of current time (GMT).
705  */
706 HV_RTCTime hv_get_rtc(void);
707
708
709 /** Set the current time-of-day clock.
710  * @param time time to reset time-of-day to (GMT).
711  */
712 void hv_set_rtc(HV_RTCTime time);
713
714 /** Installs a context, comprising a page table and other attributes.
715  *
716  *  Once this service completes, page_table will be used to translate
717  *  subsequent virtual address references to physical memory.
718  *
719  *  Installing a context does not cause an implicit TLB flush.  Before
720  *  reusing an ASID value for a different address space, the client is
721  *  expected to flush old references from the TLB with hv_flush_asid().
722  *  (Alternately, hv_flush_all() may be used to flush many ASIDs at once.)
723  *  After invalidating a page table entry, changing its attributes, or
724  *  changing its target CPA, the client is expected to flush old references
725  *  from the TLB with hv_flush_page() or hv_flush_pages(). Making a
726  *  previously invalid page valid does not require a flush.
727  *
728  *  Specifying an invalid ASID, or an invalid CPA (client physical address)
729  *  (either as page_table_pointer, or within the referenced table),
730  *  or another page table data item documented as above as illegal may
731  *  lead to client termination; since the validation of the table is
732  *  done as needed, this may happen before the service returns, or at
733  *  some later time, or never, depending upon the client's pattern of
734  *  memory references.  Page table entries which supply translations for
735  *  invalid virtual addresses may result in client termination, or may
736  *  be silently ignored.  "Invalid" in this context means a value which
737  *  was not provided to the client via the appropriate hv_inquire_* routine.
738  *
739  *  To support changing the instruction VAs at the same time as
740  *  installing the new page table, this call explicitly supports
741  *  setting the "lr" register to a different address and then jumping
742  *  directly to the hv_install_context() routine.  In this case, the
743  *  new page table does not need to contain any mapping for the
744  *  hv_install_context address itself.
745  *
746  *  At most one HV_CTX_PG_SM_* flag may be specified in "flags";
747  *  if multiple flags are specified, HV_EINVAL is returned.
748  *  Specifying none of the flags results in using the default page size.
749  *  All cores participating in a given client must request the same
750  *  page size, or the results are undefined.
751  *
752  * @param page_table Root of the page table.
753  * @param access PTE providing info on how to read the page table.  This
754  *   value must be consistent between multiple tiles sharing a page table,
755  *   and must also be consistent with any virtual mappings the client
756  *   may be using to access the page table.
757  * @param asid HV_ASID the page table is to be used for.
758  * @param flags Context flags, denoting attributes or privileges of the
759  *   current context (HV_CTX_xxx).
760  * @return Zero on success, or a hypervisor error code on failure.
761  */
762 int hv_install_context(HV_PhysAddr page_table, HV_PTE access, HV_ASID asid,
763                        __hv32 flags);
764
765 #endif /* !__ASSEMBLER__ */
766
767 #define HV_CTX_DIRECTIO     0x1   /**< Direct I/O requests are accepted from
768                                        PL0. */
769
770 #define HV_CTX_PG_SM_4K     0x10  /**< Use 4K small pages, if available. */
771 #define HV_CTX_PG_SM_16K    0x20  /**< Use 16K small pages, if available. */
772 #define HV_CTX_PG_SM_64K    0x40  /**< Use 64K small pages, if available. */
773 #define HV_CTX_PG_SM_MASK   0xf0  /**< Mask of all possible small pages. */
774
775 #ifndef __ASSEMBLER__
776
777
778 /** Set the number of pages ganged together by HV_PTE_SUPER at a
779  * particular level of the page table.
780  *
781  * The current TILE-Gx hardware only supports powers of four
782  * (i.e. log2_count must be a multiple of two), and the requested
783  * "super" page size must be less than the span of the next level in
784  * the page table.  The largest size that can be requested is 64GB.
785  *
786  * The shift value is initially "0" for all page table levels,
787  * indicating that the HV_PTE_SUPER bit is effectively ignored.
788  *
789  * If you change the count from one non-zero value to another, the
790  * hypervisor will flush the entire TLB and TSB to avoid confusion.
791  *
792  * @param level Page table level (0, 1, or 2)
793  * @param log2_count Base-2 log of the number of pages to gang together,
794  * i.e. how much to shift left the base page size for the super page size.
795  * @return Zero on success, or a hypervisor error code on failure.
796  */
797 int hv_set_pte_super_shift(int level, int log2_count);
798
799
800 /** Value returned from hv_inquire_context(). */
801 typedef struct
802 {
803   /** Physical address of page table */
804   HV_PhysAddr page_table;
805
806   /** PTE which defines access method for top of page table */
807   HV_PTE access;
808
809   /** ASID associated with this page table */
810   HV_ASID asid;
811
812   /** Context flags */
813   __hv32 flags;
814 } HV_Context;
815
816 /** Retrieve information about the currently installed context.
817  * @return The data passed to the last successful hv_install_context call.
818  */
819 HV_Context hv_inquire_context(void);
820
821
822 /** Flushes all translations associated with the named address space
823  *  identifier from the TLB and any other hypervisor data structures.
824  *  Translations installed with the "global" bit are not flushed.
825  *
826  *  Specifying an invalid ASID may lead to client termination.  "Invalid"
827  *  in this context means a value which was not provided to the client
828  *  via <tt>hv_inquire_asid()</tt>.
829  *
830  * @param asid HV_ASID whose entries are to be flushed.
831  * @return Zero on success, or a hypervisor error code on failure.
832 */
833 int hv_flush_asid(HV_ASID asid);
834
835
836 /** Flushes all translations associated with the named virtual address
837  *  and page size from the TLB and other hypervisor data structures. Only
838  *  pages visible to the current ASID are affected; note that this includes
839  *  global pages in addition to pages specific to the current ASID.
840  *
841  *  The supplied VA need not be aligned; it may be anywhere in the
842  *  subject page.
843  *
844  *  Specifying an invalid virtual address may lead to client termination,
845  *  or may silently succeed.  "Invalid" in this context means a value
846  *  which was not provided to the client via hv_inquire_virtual.
847  *
848  * @param address Address of the page to flush.
849  * @param page_size Size of pages to assume.
850  * @return Zero on success, or a hypervisor error code on failure.
851  */
852 int hv_flush_page(HV_VirtAddr address, HV_PageSize page_size);
853
854
855 /** Flushes all translations associated with the named virtual address range
856  *  and page size from the TLB and other hypervisor data structures. Only
857  *  pages visible to the current ASID are affected; note that this includes
858  *  global pages in addition to pages specific to the current ASID.
859  *
860  *  The supplied VA need not be aligned; it may be anywhere in the
861  *  subject page.
862  *
863  *  Specifying an invalid virtual address may lead to client termination,
864  *  or may silently succeed.  "Invalid" in this context means a value
865  *  which was not provided to the client via hv_inquire_virtual.
866  *
867  * @param start Address to flush.
868  * @param page_size Size of pages to assume.
869  * @param size The number of bytes to flush. Any page in the range
870  *        [start, start + size) will be flushed from the TLB.
871  * @return Zero on success, or a hypervisor error code on failure.
872  */
873 int hv_flush_pages(HV_VirtAddr start, HV_PageSize page_size,
874                    unsigned long size);
875
876
877 /** Flushes all non-global translations (if preserve_global is true),
878  *  or absolutely all translations (if preserve_global is false).
879  *
880  * @param preserve_global Non-zero if we want to preserve "global" mappings.
881  * @return Zero on success, or a hypervisor error code on failure.
882 */
883 int hv_flush_all(int preserve_global);
884
885
886 /** Restart machine with optional restart command and optional args.
887  * @param cmd Const pointer to command to restart with, or NULL
888  * @param args Const pointer to argument string to restart with, or NULL
889  */
890 void hv_restart(HV_VirtAddr cmd, HV_VirtAddr args);
891
892
893 /** Halt machine. */
894 void hv_halt(void);
895
896
897 /** Power off machine. */
898 void hv_power_off(void);
899
900
901 /** Re-enter virtual-is-physical memory translation mode and restart
902  *  execution at a given address.
903  * @param entry Client physical address at which to begin execution.
904  * @return A hypervisor error code on failure; if the operation is
905  *         successful the call does not return.
906  */
907 int hv_reexec(HV_PhysAddr entry);
908
909
910 /** Chip topology */
911 typedef struct
912 {
913   /** Relative coordinates of the querying tile */
914   HV_Coord coord;
915
916   /** Width of the querying supervisor's tile rectangle. */
917   int width;
918
919   /** Height of the querying supervisor's tile rectangle. */
920   int height;
921
922 } HV_Topology;
923
924 /** Returns information about the tile coordinate system.
925  *
926  * Each supervisor is given a rectangle of tiles it potentially controls.
927  * These tiles are labeled using a relative coordinate system with (0,0) as
928  * the upper left tile regardless of their physical location on the chip.
929  *
930  * This call returns both the size of that rectangle and the position
931  * within that rectangle of the querying tile.
932  *
933  * Not all tiles within that rectangle may be available to the supervisor;
934  * to get the precise set of available tiles, you must also call
935  * hv_inquire_tiles(HV_INQ_TILES_AVAIL, ...).
936  **/
937 HV_Topology hv_inquire_topology(void);
938
939 /** Sets of tiles we can retrieve with hv_inquire_tiles().
940  *
941  * These numbers are part of the binary API and guaranteed not to change.
942  */
943 typedef enum {
944   /** An invalid value; do not use. */
945   _HV_INQ_TILES_RESERVED       = 0,
946
947   /** All available tiles within the supervisor's tile rectangle. */
948   HV_INQ_TILES_AVAIL           = 1,
949
950   /** The set of tiles used for hash-for-home caching. */
951   HV_INQ_TILES_HFH_CACHE       = 2,
952
953   /** The set of tiles that can be legally used as a LOTAR for a PTE. */
954   HV_INQ_TILES_LOTAR           = 3
955 } HV_InqTileSet;
956
957 /** Returns specific information about various sets of tiles within the
958  *  supervisor's tile rectangle.
959  *
960  * @param set Which set of tiles to retrieve.
961  * @param cpumask Pointer to a returned bitmask (in row-major order,
962  *        supervisor-relative) of tiles.  The low bit of the first word
963  *        corresponds to the tile at the upper left-hand corner of the
964  *        supervisor's rectangle.  In order for the supervisor to know the
965  *        buffer length to supply, it should first call hv_inquire_topology.
966  * @param length Number of bytes available for the returned bitmask.
967  **/
968 HV_Errno hv_inquire_tiles(HV_InqTileSet set, HV_VirtAddr cpumask, int length);
969
970
971 /** An identifier for a memory controller. Multiple memory controllers
972  * may be connected to one chip, and this uniquely identifies each one.
973  */
974 typedef int HV_MemoryController;
975
976 /** A range of physical memory. */
977 typedef struct
978 {
979   HV_PhysAddr start;   /**< Starting address. */
980   __hv64 size;         /**< Size in bytes. */
981   HV_MemoryController controller;  /**< Which memory controller owns this. */
982 } HV_PhysAddrRange;
983
984 /** Returns information about a range of physical memory.
985  *
986  * hv_inquire_physical() returns one of the ranges of client
987  * physical addresses which are available to this client.
988  *
989  * The first range is retrieved by specifying an idx of 0, and
990  * successive ranges are returned with subsequent idx values.  Ranges
991  * are ordered by increasing start address (i.e., as idx increases,
992  * so does start), do not overlap, and do not touch (i.e., the
993  * available memory is described with the fewest possible ranges).
994  *
995  * If an out-of-range idx value is specified, the returned size will be zero.
996  * A client can count the number of ranges by increasing idx until the
997  * returned size is zero. There will always be at least one valid range.
998  *
999  * Some clients might not be prepared to deal with more than one
1000  * physical address range; they still ought to call this routine and
1001  * issue a warning message if they're given more than one range, on the
1002  * theory that whoever configured the hypervisor to provide that memory
1003  * should know that it's being wasted.
1004  */
1005 HV_PhysAddrRange hv_inquire_physical(int idx);
1006
1007 /** Possible DIMM types. */
1008 typedef enum
1009 {
1010   NO_DIMM                    = 0,  /**< No DIMM */
1011   DDR2                       = 1,  /**< DDR2 */
1012   DDR3                       = 2   /**< DDR3 */
1013 } HV_DIMM_Type;
1014
1015 #ifdef __tilegx__
1016
1017 /** Log2 of minimum DIMM bytes supported by the memory controller. */
1018 #define HV_MSH_MIN_DIMM_SIZE_SHIFT 29
1019
1020 /** Max number of DIMMs contained by one memory controller. */
1021 #define HV_MSH_MAX_DIMMS 8
1022
1023 #else
1024
1025 /** Log2 of minimum DIMM bytes supported by the memory controller. */
1026 #define HV_MSH_MIN_DIMM_SIZE_SHIFT 26
1027
1028 /** Max number of DIMMs contained by one memory controller. */
1029 #define HV_MSH_MAX_DIMMS 2
1030
1031 #endif
1032
1033 /** Number of bits to right-shift to get the DIMM type. */
1034 #define HV_DIMM_TYPE_SHIFT 0
1035
1036 /** Bits to mask to get the DIMM type. */
1037 #define HV_DIMM_TYPE_MASK 0xf
1038
1039 /** Number of bits to right-shift to get the DIMM size. */
1040 #define HV_DIMM_SIZE_SHIFT 4
1041
1042 /** Bits to mask to get the DIMM size. */
1043 #define HV_DIMM_SIZE_MASK 0xf
1044
1045 /** Memory controller information. */
1046 typedef struct
1047 {
1048   HV_Coord coord;   /**< Relative tile coordinates of the port used by a
1049                          specified tile to communicate with this controller. */
1050   __hv64 speed;     /**< Speed of this controller in bytes per second. */
1051 } HV_MemoryControllerInfo;
1052
1053 /** Returns information about a particular memory controller.
1054  *
1055  *  hv_inquire_memory_controller(coord,idx) returns information about a
1056  *  particular controller.  Two pieces of information are returned:
1057  *  - The relative coordinates of the port on the controller that the specified
1058  *    tile would use to contact it.  The relative coordinates may lie
1059  *    outside the supervisor's rectangle, i.e. the controller may not
1060  *    be attached to a node managed by the querying node's supervisor.
1061  *    In particular note that x or y may be negative.
1062  *  - The speed of the memory controller.  (This is a not-to-exceed value
1063  *    based on the raw hardware data rate, and may not be achievable in
1064  *    practice; it is provided to give clients information on the relative
1065  *    performance of the available controllers.)
1066  *
1067  *  Clients should avoid calling this interface with invalid values.
1068  *  A client who does may be terminated.
1069  * @param coord Tile for which to calculate the relative port position.
1070  * @param controller Index of the controller; identical to value returned
1071  *        from other routines like hv_inquire_physical.
1072  * @return Information about the controller.
1073  */
1074 HV_MemoryControllerInfo hv_inquire_memory_controller(HV_Coord coord,
1075                                                      int controller);
1076
1077
1078 /** A range of virtual memory. */
1079 typedef struct
1080 {
1081   HV_VirtAddr start;   /**< Starting address. */
1082   __hv64 size;         /**< Size in bytes. */
1083 } HV_VirtAddrRange;
1084
1085 /** Returns information about a range of virtual memory.
1086  *
1087  * hv_inquire_virtual() returns one of the ranges of client
1088  * virtual addresses which are available to this client.
1089  *
1090  * The first range is retrieved by specifying an idx of 0, and
1091  * successive ranges are returned with subsequent idx values.  Ranges
1092  * are ordered by increasing start address (i.e., as idx increases,
1093  * so does start), do not overlap, and do not touch (i.e., the
1094  * available memory is described with the fewest possible ranges).
1095  *
1096  * If an out-of-range idx value is specified, the returned size will be zero.
1097  * A client can count the number of ranges by increasing idx until the
1098  * returned size is zero. There will always be at least one valid range.
1099  *
1100  * Some clients may well have various virtual addresses hardwired
1101  * into themselves; for instance, their instruction stream may
1102  * have been compiled expecting to live at a particular address.
1103  * Such clients should use this interface to verify they've been
1104  * given the virtual address space they expect, and issue a (potentially
1105  * fatal) warning message otherwise.
1106  *
1107  * Note that the returned size is a __hv64, not a __hv32, so it is
1108  * possible to express a single range spanning the entire 32-bit
1109  * address space.
1110  */
1111 HV_VirtAddrRange hv_inquire_virtual(int idx);
1112
1113
1114 /** A range of ASID values. */
1115 typedef struct
1116 {
1117   HV_ASID start;        /**< First ASID in the range. */
1118   unsigned int size;    /**< Number of ASIDs. Zero for an invalid range. */
1119 } HV_ASIDRange;
1120
1121 /** Returns information about a range of ASIDs.
1122  *
1123  * hv_inquire_asid() returns one of the ranges of address
1124  * space identifiers which are available to this client.
1125  *
1126  * The first range is retrieved by specifying an idx of 0, and
1127  * successive ranges are returned with subsequent idx values.  Ranges
1128  * are ordered by increasing start value (i.e., as idx increases,
1129  * so does start), do not overlap, and do not touch (i.e., the
1130  * available ASIDs are described with the fewest possible ranges).
1131  *
1132  * If an out-of-range idx value is specified, the returned size will be zero.
1133  * A client can count the number of ranges by increasing idx until the
1134  * returned size is zero. There will always be at least one valid range.
1135  */
1136 HV_ASIDRange hv_inquire_asid(int idx);
1137
1138
1139 /** Waits for at least the specified number of nanoseconds then returns.
1140  *
1141  * NOTE: this deprecated function currently assumes a 750 MHz clock,
1142  * and is thus not generally suitable for use.  New code should call
1143  * hv_sysconf(HV_SYSCONF_CPU_SPEED), compute a cycle count to wait for,
1144  * and delay by looping while checking the cycle counter SPR.
1145  *
1146  * @param nanosecs The number of nanoseconds to sleep.
1147  */
1148 void hv_nanosleep(int nanosecs);
1149
1150
1151 /** Reads a character from the console without blocking.
1152  *
1153  * @return A value from 0-255 indicates the value successfully read.
1154  * A negative value means no value was ready.
1155  */
1156 int hv_console_read_if_ready(void);
1157
1158
1159 /** Writes a character to the console, blocking if the console is busy.
1160  *
1161  *  This call cannot fail. If the console is broken for some reason,
1162  *  output will simply vanish.
1163  * @param byte Character to write.
1164  */
1165 void hv_console_putc(int byte);
1166
1167
1168 /** Writes a string to the console, blocking if the console is busy.
1169  * @param bytes Pointer to characters to write.
1170  * @param len Number of characters to write.
1171  * @return Number of characters written, or HV_EFAULT if the buffer is invalid.
1172  */
1173 int hv_console_write(HV_VirtAddr bytes, int len);
1174
1175
1176 /** Dispatch the next interrupt from the client downcall mechanism.
1177  *
1178  *  The hypervisor uses downcalls to notify the client of asynchronous
1179  *  events.  Some of these events are hypervisor-created (like incoming
1180  *  messages).  Some are regular interrupts which initially occur in
1181  *  the hypervisor, and are normally handled directly by the client;
1182  *  when these occur in a client's interrupt critical section, they must
1183  *  be delivered through the downcall mechanism.
1184  *
1185  *  A downcall is initially delivered to the client as an INTCTRL_CL
1186  *  interrupt, where CL is the client's PL.  Upon entry to the INTCTRL_CL
1187  *  vector, the client must immediately invoke the hv_downcall_dispatch
1188  *  service.  This service will not return; instead it will cause one of
1189  *  the client's actual downcall-handling interrupt vectors to be entered.
1190  *  The EX_CONTEXT registers in the client will be set so that when the
1191  *  client irets, it will return to the code which was interrupted by the
1192  *  INTCTRL_CL interrupt.
1193  *
1194  *  Under some circumstances, the firing of INTCTRL_CL can race with
1195  *  the lowering of a device interrupt.  In such a case, the
1196  *  hv_downcall_dispatch service may issue an iret instruction instead
1197  *  of entering one of the client's actual downcall-handling interrupt
1198  *  vectors.  This will return execution to the location that was
1199  *  interrupted by INTCTRL_CL.
1200  *
1201  *  Any saving of registers should be done by the actual handling
1202  *  vectors; no registers should be changed by the INTCTRL_CL handler.
1203  *  In particular, the client should not use a jal instruction to invoke
1204  *  the hv_downcall_dispatch service, as that would overwrite the client's
1205  *  lr register.  Note that the hv_downcall_dispatch service may overwrite
1206  *  one or more of the client's system save registers.
1207  *
1208  *  The client must not modify the INTCTRL_CL_STATUS SPR.  The hypervisor
1209  *  will set this register to cause a downcall to happen, and will clear
1210  *  it when no further downcalls are pending.
1211  *
1212  *  When a downcall vector is entered, the INTCTRL_CL interrupt will be
1213  *  masked.  When the client is done processing a downcall, and is ready
1214  *  to accept another, it must unmask this interrupt; if more downcalls
1215  *  are pending, this will cause the INTCTRL_CL vector to be reentered.
1216  *  Currently the following interrupt vectors can be entered through a
1217  *  downcall:
1218  *
1219  *  INT_MESSAGE_RCV_DWNCL   (hypervisor message available)
1220  *  INT_DEV_INTR_DWNCL      (device interrupt)
1221  *  INT_DMATLB_MISS_DWNCL   (DMA TLB miss)
1222  *  INT_SNITLB_MISS_DWNCL   (SNI TLB miss)
1223  *  INT_DMATLB_ACCESS_DWNCL (DMA TLB access violation)
1224  */
1225 void hv_downcall_dispatch(void);
1226
1227 #endif /* !__ASSEMBLER__ */
1228
1229 /** We use actual interrupt vectors which never occur (they're only there
1230  *  to allow setting MPLs for related SPRs) for our downcall vectors.
1231  */
1232 /** Message receive downcall interrupt vector */
1233 #define INT_MESSAGE_RCV_DWNCL    INT_BOOT_ACCESS
1234 /** DMA TLB miss downcall interrupt vector */
1235 #define INT_DMATLB_MISS_DWNCL    INT_DMA_ASID
1236 /** Static nework processor instruction TLB miss interrupt vector */
1237 #define INT_SNITLB_MISS_DWNCL    INT_SNI_ASID
1238 /** DMA TLB access violation downcall interrupt vector */
1239 #define INT_DMATLB_ACCESS_DWNCL  INT_DMA_CPL
1240 /** Device interrupt downcall interrupt vector */
1241 #define INT_DEV_INTR_DWNCL       INT_WORLD_ACCESS
1242
1243 #ifndef __ASSEMBLER__
1244
1245 /** Requests the inode for a specific full pathname.
1246  *
1247  * Performs a lookup in the hypervisor filesystem for a given filename.
1248  * Multiple calls with the same filename will always return the same inode.
1249  * If there is no such filename, HV_ENOENT is returned.
1250  * A bad filename pointer may result in HV_EFAULT instead.
1251  *
1252  * @param filename Constant pointer to name of requested file
1253  * @return Inode of requested file
1254  */
1255 int hv_fs_findfile(HV_VirtAddr filename);
1256
1257
1258 /** Data returned from an fstat request.
1259  * Note that this structure should be no more than 40 bytes in size so
1260  * that it can always be returned completely in registers.
1261  */
1262 typedef struct
1263 {
1264   int size;             /**< Size of file (or HV_Errno on error) */
1265   unsigned int flags;   /**< Flags (see HV_FS_FSTAT_FLAGS) */
1266 } HV_FS_StatInfo;
1267
1268 /** Bitmask flags for fstat request */
1269 typedef enum
1270 {
1271   HV_FS_ISDIR    = 0x0001   /**< Is the entry a directory? */
1272 } HV_FS_FSTAT_FLAGS;
1273
1274 /** Get stat information on a given file inode.
1275  *
1276  * Return information on the file with the given inode.
1277  *
1278  * IF the HV_FS_ISDIR bit is set, the "file" is a directory.  Reading
1279  * it will return NUL-separated filenames (no directory part) relative
1280  * to the path to the inode of the directory "file".  These can be
1281  * appended to the path to the directory "file" after a forward slash
1282  * to create additional filenames.  Note that it is not required
1283  * that all valid paths be decomposable into valid parent directories;
1284  * a filesystem may validly have just a few files, none of which have
1285  * HV_FS_ISDIR set.  However, if clients may wish to enumerate the
1286  * files in the filesystem, it is recommended to include all the
1287  * appropriate parent directory "files" to give a consistent view.
1288  *
1289  * An invalid file inode will cause an HV_EBADF error to be returned.
1290  *
1291  * @param inode The inode number of the query
1292  * @return An HV_FS_StatInfo structure
1293  */
1294 HV_FS_StatInfo hv_fs_fstat(int inode);
1295
1296
1297 /** Read data from a specific hypervisor file.
1298  * On error, may return HV_EBADF for a bad inode or HV_EFAULT for a bad buf.
1299  * Reads near the end of the file will return fewer bytes than requested.
1300  * Reads at or beyond the end of a file will return zero.
1301  *
1302  * @param inode the hypervisor file to read
1303  * @param buf the buffer to read data into
1304  * @param length the number of bytes of data to read
1305  * @param offset the offset into the file to read the data from
1306  * @return number of bytes successfully read, or an HV_Errno code
1307  */
1308 int hv_fs_pread(int inode, HV_VirtAddr buf, int length, int offset);
1309
1310
1311 /** Read a 64-bit word from the specified physical address.
1312  * The address must be 8-byte aligned.
1313  * Specifying an invalid physical address will lead to client termination.
1314  * @param addr The physical address to read
1315  * @param access The PTE describing how to read the memory
1316  * @return The 64-bit value read from the given address
1317  */
1318 unsigned long long hv_physaddr_read64(HV_PhysAddr addr, HV_PTE access);
1319
1320
1321 /** Write a 64-bit word to the specified physical address.
1322  * The address must be 8-byte aligned.
1323  * Specifying an invalid physical address will lead to client termination.
1324  * @param addr The physical address to write
1325  * @param access The PTE that says how to write the memory
1326  * @param val The 64-bit value to write to the given address
1327  */
1328 void hv_physaddr_write64(HV_PhysAddr addr, HV_PTE access,
1329                          unsigned long long val);
1330
1331
1332 /** Get the value of the command-line for the supervisor, if any.
1333  * This will not include the filename of the booted supervisor, but may
1334  * include configured-in boot arguments or the hv_restart() arguments.
1335  * If the buffer is not long enough the hypervisor will NUL the first
1336  * character of the buffer but not write any other data.
1337  * @param buf The virtual address to write the command-line string to.
1338  * @param length The length of buf, in characters.
1339  * @return The actual length of the command line, including the trailing NUL
1340  *         (may be larger than "length").
1341  */
1342 int hv_get_command_line(HV_VirtAddr buf, int length);
1343
1344
1345 /** Set a new value for the command-line for the supervisor, which will
1346  *  be returned from subsequent invocations of hv_get_command_line() on
1347  *  this tile.
1348  * @param buf The virtual address to read the command-line string from.
1349  * @param length The length of buf, in characters; must be no more than
1350  *        HV_COMMAND_LINE_LEN.
1351  * @return Zero if successful, or a hypervisor error code.
1352  */
1353 HV_Errno hv_set_command_line(HV_VirtAddr buf, int length);
1354
1355 /** Maximum size of a command line passed to hv_set_command_line(); note
1356  *  that a line returned from hv_get_command_line() could be larger than
1357  *  this.*/
1358 #define HV_COMMAND_LINE_LEN  256
1359
1360 /** Tell the hypervisor how to cache non-priority pages
1361  * (its own as well as pages explicitly represented in page tables).
1362  * Normally these will be represented as red/black pages, but
1363  * when the supervisor starts to allocate "priority" pages in the PTE
1364  * the hypervisor will need to start marking those pages as (e.g.) "red"
1365  * and non-priority pages as either "black" (if they cache-alias
1366  * with the existing priority pages) or "red/black" (if they don't).
1367  * The bitmask provides information on which parts of the cache
1368  * have been used for pinned pages so far on this tile; if (1 << N)
1369  * appears in the bitmask, that indicates that a 4KB region of the
1370  * cache starting at (N * 4KB) is in use by a "priority" page.
1371  * The portion of cache used by a particular page can be computed
1372  * by taking the page's PA, modulo CHIP_L2_CACHE_SIZE(), and setting
1373  * all the "4KB" bits corresponding to the actual page size.
1374  * @param bitmask A bitmap of priority page set values
1375  */
1376 void hv_set_caching(unsigned long bitmask);
1377
1378
1379 /** Zero out a specified number of pages.
1380  * The va and size must both be multiples of 4096.
1381  * Caches are bypassed and memory is directly set to zero.
1382  * This API is implemented only in the magic hypervisor and is intended
1383  * to provide a performance boost to the minimal supervisor by
1384  * giving it a fast way to zero memory pages when allocating them.
1385  * @param va Virtual address where the page has been mapped
1386  * @param size Number of bytes (must be a page size multiple)
1387  */
1388 void hv_bzero_page(HV_VirtAddr va, unsigned int size);
1389
1390
1391 /** State object for the hypervisor messaging subsystem. */
1392 typedef struct
1393 {
1394 #if CHIP_VA_WIDTH() > 32
1395   __hv64 opaque[2]; /**< No user-serviceable parts inside */
1396 #else
1397   __hv32 opaque[2]; /**< No user-serviceable parts inside */
1398 #endif
1399 }
1400 HV_MsgState;
1401
1402 /** Register to receive incoming messages.
1403  *
1404  *  This routine configures the current tile so that it can receive
1405  *  incoming messages.  It must be called before the client can receive
1406  *  messages with the hv_receive_message routine, and must be called on
1407  *  each tile which will receive messages.
1408  *
1409  *  msgstate is the virtual address of a state object of type HV_MsgState.
1410  *  Once the state is registered, the client must not read or write the
1411  *  state object; doing so will cause undefined results.
1412  *
1413  *  If this routine is called with msgstate set to 0, the client's message
1414  *  state will be freed and it will no longer be able to receive messages.
1415  *  Note that this may cause the loss of any as-yet-undelivered messages
1416  *  for the client.
1417  *
1418  *  If another client attempts to send a message to a client which has
1419  *  not yet called hv_register_message_state, or which has freed its
1420  *  message state, the message will not be delivered, as if the client
1421  *  had insufficient buffering.
1422  *
1423  *  This routine returns HV_OK if the registration was successful, and
1424  *  HV_EINVAL if the supplied state object is unsuitable.  Note that some
1425  *  errors may not be detected during this routine, but might be detected
1426  *  during a subsequent message delivery.
1427  * @param msgstate State object.
1428  **/
1429 HV_Errno hv_register_message_state(HV_MsgState* msgstate);
1430
1431 /** Possible message recipient states. */
1432 typedef enum
1433 {
1434   HV_TO_BE_SENT,    /**< Not sent (not attempted, or recipient not ready) */
1435   HV_SENT,          /**< Successfully sent */
1436   HV_BAD_RECIP      /**< Bad recipient coordinates (permanent error) */
1437 } HV_Recip_State;
1438
1439 /** Message recipient. */
1440 typedef struct
1441 {
1442   /** X coordinate, relative to supervisor's top-left coordinate */
1443   unsigned int x:11;
1444
1445   /** Y coordinate, relative to supervisor's top-left coordinate */
1446   unsigned int y:11;
1447
1448   /** Status of this recipient */
1449   HV_Recip_State state:10;
1450 } HV_Recipient;
1451
1452 /** Send a message to a set of recipients.
1453  *
1454  *  This routine sends a message to a set of recipients.
1455  *
1456  *  recips is an array of HV_Recipient structures.  Each specifies a tile,
1457  *  and a message state; initially, it is expected that the state will
1458  *  be set to HV_TO_BE_SENT.  nrecip specifies the number of recipients
1459  *  in the recips array.
1460  *
1461  *  For each recipient whose state is HV_TO_BE_SENT, the hypervisor attempts
1462  *  to send that tile the specified message.  In order to successfully
1463  *  receive the message, the receiver must be a valid tile to which the
1464  *  sender has access, must not be the sending tile itself, and must have
1465  *  sufficient free buffer space.  (The hypervisor guarantees that each
1466  *  tile which has called hv_register_message_state() will be able to
1467  *  buffer one message from every other tile which can legally send to it;
1468  *  more space may be provided but is not guaranteed.)  If an invalid tile
1469  *  is specified, the recipient's state is set to HV_BAD_RECIP; this is a
1470  *  permanent delivery error.  If the message is successfully delivered
1471  *  to the recipient's buffer, the recipient's state is set to HV_SENT.
1472  *  Otherwise, the recipient's state is unchanged.  Message delivery is
1473  *  synchronous; all attempts to send messages are completed before this
1474  *  routine returns.
1475  *
1476  *  If no permanent delivery errors were encountered, the routine returns
1477  *  the number of messages successfully sent: that is, the number of
1478  *  recipients whose states changed from HV_TO_BE_SENT to HV_SENT during
1479  *  this operation.  If any permanent delivery errors were encountered,
1480  *  the routine returns HV_ERECIP.  In the event of permanent delivery
1481  *  errors, it may be the case that delivery was not attempted to all
1482  *  recipients; if any messages were successfully delivered, however,
1483  *  recipients' state values will be updated appropriately.
1484  *
1485  *  It is explicitly legal to specify a recipient structure whose state
1486  *  is not HV_TO_BE_SENT; such a recipient is ignored.  One suggested way
1487  *  of using hv_send_message to send a message to multiple tiles is to set
1488  *  up a list of recipients, and then call the routine repeatedly with the
1489  *  same list, each time accumulating the number of messages successfully
1490  *  sent, until all messages are sent, a permanent error is encountered,
1491  *  or the desired number of attempts have been made.  When used in this
1492  *  way, the routine will deliver each message no more than once to each
1493  *  recipient.
1494  *
1495  *  Note that a message being successfully delivered to the recipient's
1496  *  buffer space does not guarantee that it is received by the recipient,
1497  *  either immediately or at any time in the future; the recipient might
1498  *  never call hv_receive_message, or could register a different state
1499  *  buffer, losing the message.
1500  *
1501  *  Specifying the same recipient more than once in the recipient list
1502  *  is an error, which will not result in an error return but which may
1503  *  or may not result in more than one message being delivered to the
1504  *  recipient tile.
1505  *
1506  *  buf and buflen specify the message to be sent.  buf is a virtual address
1507  *  which must be currently mapped in the client's page table; if not, the
1508  *  routine returns HV_EFAULT.  buflen must be greater than zero and less
1509  *  than or equal to HV_MAX_MESSAGE_SIZE, and nrecip must be less than the
1510  *  number of tiles to which the sender has access; if not, the routine
1511  *  returns HV_EINVAL.
1512  * @param recips List of recipients.
1513  * @param nrecip Number of recipients.
1514  * @param buf Address of message data.
1515  * @param buflen Length of message data.
1516  **/
1517 int hv_send_message(HV_Recipient *recips, int nrecip,
1518                     HV_VirtAddr buf, int buflen);
1519
1520 /** Maximum hypervisor message size, in bytes */
1521 #define HV_MAX_MESSAGE_SIZE 28
1522
1523
1524 /** Return value from hv_receive_message() */
1525 typedef struct
1526 {
1527   int msglen;     /**< Message length in bytes, or an error code */
1528   __hv32 source;  /**< Code identifying message sender (HV_MSG_xxx) */
1529 } HV_RcvMsgInfo;
1530
1531 #define HV_MSG_TILE 0x0         /**< Message source is another tile */
1532 #define HV_MSG_INTR 0x1         /**< Message source is a driver interrupt */
1533
1534 /** Receive a message.
1535  *
1536  * This routine retrieves a message from the client's incoming message
1537  * buffer.
1538  *
1539  * Multiple messages sent from a particular sending tile to a particular
1540  * receiving tile are received in the order that they were sent; however,
1541  * no ordering is guaranteed between messages sent by different tiles.
1542  *
1543  * Whenever the a client's message buffer is empty, the first message
1544  * subsequently received will cause the client's MESSAGE_RCV_DWNCL
1545  * interrupt vector to be invoked through the interrupt downcall mechanism
1546  * (see the description of the hv_downcall_dispatch() routine for details
1547  * on downcalls).
1548  *
1549  * Another message-available downcall will not occur until a call to
1550  * this routine is made when the message buffer is empty, and a message
1551  * subsequently arrives.  Note that such a downcall could occur while
1552  * this routine is executing.  If the calling code does not wish this
1553  * to happen, it is recommended that this routine be called with the
1554  * INTCTRL_1 interrupt masked, or inside an interrupt critical section.
1555  *
1556  * msgstate is the value previously passed to hv_register_message_state().
1557  * buf is the virtual address of the buffer into which the message will
1558  * be written; buflen is the length of the buffer.
1559  *
1560  * This routine returns an HV_RcvMsgInfo structure.  The msglen member
1561  * of that structure is the length of the message received, zero if no
1562  * message is available, or HV_E2BIG if the message is too large for the
1563  * specified buffer.  If the message is too large, it is not consumed,
1564  * and may be retrieved by a subsequent call to this routine specifying
1565  * a sufficiently large buffer.  A buffer which is HV_MAX_MESSAGE_SIZE
1566  * bytes long is guaranteed to be able to receive any possible message.
1567  *
1568  * The source member of the HV_RcvMsgInfo structure describes the sender
1569  * of the message.  For messages sent by another client tile via an
1570  * hv_send_message() call, this value is HV_MSG_TILE; for messages sent
1571  * as a result of a device interrupt, this value is HV_MSG_INTR.
1572  */
1573
1574 HV_RcvMsgInfo hv_receive_message(HV_MsgState msgstate, HV_VirtAddr buf,
1575                                  int buflen);
1576
1577
1578 /** Start remaining tiles owned by this supervisor.  Initially, only one tile
1579  *  executes the client program; after it calls this service, the other tiles
1580  *  are started.  This allows the initial tile to do one-time configuration
1581  *  of shared data structures without having to lock them against simultaneous
1582  *  access.
1583  */
1584 void hv_start_all_tiles(void);
1585
1586
1587 /** Open a hypervisor device.
1588  *
1589  *  This service initializes an I/O device and its hypervisor driver software,
1590  *  and makes it available for use.  The open operation is per-device per-chip;
1591  *  once it has been performed, the device handle returned may be used in other
1592  *  device services calls made by any tile.
1593  *
1594  * @param name Name of the device.  A base device name is just a text string
1595  *        (say, "pcie").  If there is more than one instance of a device, the
1596  *        base name is followed by a slash and a device number (say, "pcie/0").
1597  *        Some devices may support further structure beneath those components;
1598  *        most notably, devices which require control operations do so by
1599  *        supporting reads and/or writes to a control device whose name
1600  *        includes a trailing "/ctl" (say, "pcie/0/ctl").
1601  * @param flags Flags (HV_DEV_xxx).
1602  * @return A positive integer device handle, or a negative error code.
1603  */
1604 int hv_dev_open(HV_VirtAddr name, __hv32 flags);
1605
1606
1607 /** Close a hypervisor device.
1608  *
1609  *  This service uninitializes an I/O device and its hypervisor driver
1610  *  software, and makes it unavailable for use.  The close operation is
1611  *  per-device per-chip; once it has been performed, the device is no longer
1612  *  available.  Normally there is no need to ever call the close service.
1613  *
1614  * @param devhdl Device handle of the device to be closed.
1615  * @return Zero if the close is successful, otherwise, a negative error code.
1616  */
1617 int hv_dev_close(int devhdl);
1618
1619
1620 /** Read data from a hypervisor device synchronously.
1621  *
1622  *  This service transfers data from a hypervisor device to a memory buffer.
1623  *  When the service returns, the data has been written from the memory buffer,
1624  *  and the buffer will not be further modified by the driver.
1625  *
1626  *  No ordering is guaranteed between requests issued from different tiles.
1627  *
1628  *  Devices may choose to support both the synchronous and asynchronous read
1629  *  operations, only one of them, or neither of them.
1630  *
1631  * @param devhdl Device handle of the device to be read from.
1632  * @param flags Flags (HV_DEV_xxx).
1633  * @param va Virtual address of the target data buffer.  This buffer must
1634  *        be mapped in the currently installed page table; if not, HV_EFAULT
1635  *        may be returned.
1636  * @param len Number of bytes to be transferred.
1637  * @param offset Driver-dependent offset.  For a random-access device, this is
1638  *        often a byte offset from the beginning of the device; in other cases,
1639  *        like on a control device, it may have a different meaning.
1640  * @return A non-negative value if the read was at least partially successful;
1641  *         otherwise, a negative error code.  The precise interpretation of
1642  *         the return value is driver-dependent, but many drivers will return
1643  *         the number of bytes successfully transferred.
1644  */
1645 int hv_dev_pread(int devhdl, __hv32 flags, HV_VirtAddr va, __hv32 len,
1646                  __hv64 offset);
1647
1648 #define HV_DEV_NB_EMPTY     0x1   /**< Don't block when no bytes of data can
1649                                        be transferred. */
1650 #define HV_DEV_NB_PARTIAL   0x2   /**< Don't block when some bytes, but not all
1651                                        of the requested bytes, can be
1652                                        transferred. */
1653 #define HV_DEV_NOCACHE      0x4   /**< The caller warrants that none of the
1654                                        cache lines which might contain data
1655                                        from the requested buffer are valid.
1656                                        Useful with asynchronous operations
1657                                        only. */
1658
1659 #define HV_DEV_ALLFLAGS     (HV_DEV_NB_EMPTY | HV_DEV_NB_PARTIAL | \
1660                              HV_DEV_NOCACHE)   /**< All HV_DEV_xxx flags */
1661
1662 /** Write data to a hypervisor device synchronously.
1663  *
1664  *  This service transfers data from a memory buffer to a hypervisor device.
1665  *  When the service returns, the data has been read from the memory buffer,
1666  *  and the buffer may be overwritten by the client; the data may not
1667  *  necessarily have been conveyed to the actual hardware I/O interface.
1668  *
1669  *  No ordering is guaranteed between requests issued from different tiles.
1670  *
1671  *  Devices may choose to support both the synchronous and asynchronous write
1672  *  operations, only one of them, or neither of them.
1673  *
1674  * @param devhdl Device handle of the device to be written to.
1675  * @param flags Flags (HV_DEV_xxx).
1676  * @param va Virtual address of the source data buffer.  This buffer must
1677  *        be mapped in the currently installed page table; if not, HV_EFAULT
1678  *        may be returned.
1679  * @param len Number of bytes to be transferred.
1680  * @param offset Driver-dependent offset.  For a random-access device, this is
1681  *        often a byte offset from the beginning of the device; in other cases,
1682  *        like on a control device, it may have a different meaning.
1683  * @return A non-negative value if the write was at least partially successful;
1684  *         otherwise, a negative error code.  The precise interpretation of
1685  *         the return value is driver-dependent, but many drivers will return
1686  *         the number of bytes successfully transferred.
1687  */
1688 int hv_dev_pwrite(int devhdl, __hv32 flags, HV_VirtAddr va, __hv32 len,
1689                   __hv64 offset);
1690
1691
1692 /** Interrupt arguments, used in the asynchronous I/O interfaces. */
1693 #if CHIP_VA_WIDTH() > 32
1694 typedef __hv64 HV_IntArg;
1695 #else
1696 typedef __hv32 HV_IntArg;
1697 #endif
1698
1699 /** Interrupt messages are delivered via the mechanism as normal messages,
1700  *  but have a message source of HV_DEV_INTR.  The message is formatted
1701  *  as an HV_IntrMsg structure.
1702  */
1703
1704 typedef struct
1705 {
1706   HV_IntArg intarg;  /**< Interrupt argument, passed to the poll/preada/pwritea
1707                           services */
1708   HV_IntArg intdata; /**< Interrupt-specific interrupt data */
1709 } HV_IntrMsg;
1710
1711 /** Request an interrupt message when a device condition is satisfied.
1712  *
1713  *  This service requests that an interrupt message be delivered to the
1714  *  requesting tile when a device becomes readable or writable, or when any
1715  *  data queued to the device via previous write operations from this tile
1716  *  has been actually sent out on the hardware I/O interface.  Devices may
1717  *  choose to support any, all, or none of the available conditions.
1718  *
1719  *  If multiple conditions are specified, only one message will be
1720  *  delivered.  If the event mask delivered to that interrupt handler
1721  *  indicates that some of the conditions have not yet occurred, the
1722  *  client must issue another poll() call if it wishes to wait for those
1723  *  conditions.
1724  *
1725  *  Only one poll may be outstanding per device handle per tile.  If more than
1726  *  one tile is polling on the same device and condition, they will all be
1727  *  notified when it happens.  Because of this, clients may not assume that
1728  *  the condition signaled is necessarily still true when they request a
1729  *  subsequent service; for instance, the readable data which caused the
1730  *  poll call to interrupt may have been read by another tile in the interim.
1731  *
1732  *  The notification interrupt message could come directly, or via the
1733  *  downcall (intctrl1) method, depending on what the tile is doing
1734  *  when the condition is satisfied.  Note that it is possible for the
1735  *  requested interrupt to be delivered after this service is called but
1736  *  before it returns.
1737  *
1738  * @param devhdl Device handle of the device to be polled.
1739  * @param events Flags denoting the events which will cause the interrupt to
1740  *        be delivered (HV_DEVPOLL_xxx).
1741  * @param intarg Value which will be delivered as the intarg member of the
1742  *        eventual interrupt message; the intdata member will be set to a
1743  *        mask of HV_DEVPOLL_xxx values indicating which conditions have been
1744  *        satisifed.
1745  * @return Zero if the interrupt was successfully scheduled; otherwise, a
1746  *         negative error code.
1747  */
1748 int hv_dev_poll(int devhdl, __hv32 events, HV_IntArg intarg);
1749
1750 #define HV_DEVPOLL_READ     0x1   /**< Test device for readability */
1751 #define HV_DEVPOLL_WRITE    0x2   /**< Test device for writability */
1752 #define HV_DEVPOLL_FLUSH    0x4   /**< Test device for output drained */
1753
1754
1755 /** Cancel a request for an interrupt when a device event occurs.
1756  *
1757  *  This service requests that no interrupt be delivered when the events
1758  *  noted in the last-issued poll() call happen.  Once this service returns,
1759  *  the interrupt has been canceled; however, it is possible for the interrupt
1760  *  to be delivered after this service is called but before it returns.
1761  *
1762  * @param devhdl Device handle of the device on which to cancel polling.
1763  * @return Zero if the poll was successfully canceled; otherwise, a negative
1764  *         error code.
1765  */
1766 int hv_dev_poll_cancel(int devhdl);
1767
1768
1769 /** Scatter-gather list for preada/pwritea calls. */
1770 typedef struct
1771 #if CHIP_VA_WIDTH() <= 32
1772 __attribute__ ((packed, aligned(4)))
1773 #endif
1774 {
1775   HV_PhysAddr pa;  /**< Client physical address of the buffer segment. */
1776   HV_PTE pte;      /**< Page table entry describing the caching and location
1777                         override characteristics of the buffer segment.  Some
1778                         drivers ignore this element and will require that
1779                         the NOCACHE flag be set on their requests. */
1780   __hv32 len;      /**< Length of the buffer segment. */
1781 } HV_SGL;
1782
1783 #define HV_SGL_MAXLEN 16  /**< Maximum number of entries in a scatter-gather
1784                                list */
1785
1786 /** Read data from a hypervisor device asynchronously.
1787  *
1788  *  This service transfers data from a hypervisor device to a memory buffer.
1789  *  When the service returns, the read has been scheduled.  When the read
1790  *  completes, an interrupt message will be delivered, and the buffer will
1791  *  not be further modified by the driver.
1792  *
1793  *  The number of possible outstanding asynchronous requests is defined by
1794  *  each driver, but it is recommended that it be at least two requests
1795  *  per tile per device.
1796  *
1797  *  No ordering is guaranteed between synchronous and asynchronous requests,
1798  *  even those issued on the same tile.
1799  *
1800  *  The completion interrupt message could come directly, or via the downcall
1801  *  (intctrl1) method, depending on what the tile is doing when the read
1802  *  completes.  Interrupts do not coalesce; one is delivered for each
1803  *  asynchronous I/O request.  Note that it is possible for the requested
1804  *  interrupt to be delivered after this service is called but before it
1805  *  returns.
1806  *
1807  *  Devices may choose to support both the synchronous and asynchronous read
1808  *  operations, only one of them, or neither of them.
1809  *
1810  * @param devhdl Device handle of the device to be read from.
1811  * @param flags Flags (HV_DEV_xxx).
1812  * @param sgl_len Number of elements in the scatter-gather list.
1813  * @param sgl Scatter-gather list describing the memory to which data will be
1814  *        written.
1815  * @param offset Driver-dependent offset.  For a random-access device, this is
1816  *        often a byte offset from the beginning of the device; in other cases,
1817  *        like on a control device, it may have a different meaning.
1818  * @param intarg Value which will be delivered as the intarg member of the
1819  *        eventual interrupt message; the intdata member will be set to the
1820  *        normal return value from the read request.
1821  * @return Zero if the read was successfully scheduled; otherwise, a negative
1822  *         error code.  Note that some drivers may choose to pre-validate
1823  *         their arguments, and may thus detect certain device error
1824  *         conditions at this time rather than when the completion notification
1825  *         occurs, but this is not required.
1826  */
1827 int hv_dev_preada(int devhdl, __hv32 flags, __hv32 sgl_len,
1828                   HV_SGL sgl[/* sgl_len */], __hv64 offset, HV_IntArg intarg);
1829
1830
1831 /** Write data to a hypervisor device asynchronously.
1832  *
1833  *  This service transfers data from a memory buffer to a hypervisor
1834  *  device.  When the service returns, the write has been scheduled.
1835  *  When the write completes, an interrupt message will be delivered,
1836  *  and the buffer may be overwritten by the client; the data may not
1837  *  necessarily have been conveyed to the actual hardware I/O interface.
1838  *
1839  *  The number of possible outstanding asynchronous requests is defined by
1840  *  each driver, but it is recommended that it be at least two requests
1841  *  per tile per device.
1842  *
1843  *  No ordering is guaranteed between synchronous and asynchronous requests,
1844  *  even those issued on the same tile.
1845  *
1846  *  The completion interrupt message could come directly, or via the downcall
1847  *  (intctrl1) method, depending on what the tile is doing when the read
1848  *  completes.  Interrupts do not coalesce; one is delivered for each
1849  *  asynchronous I/O request.  Note that it is possible for the requested
1850  *  interrupt to be delivered after this service is called but before it
1851  *  returns.
1852  *
1853  *  Devices may choose to support both the synchronous and asynchronous write
1854  *  operations, only one of them, or neither of them.
1855  *
1856  * @param devhdl Device handle of the device to be read from.
1857  * @param flags Flags (HV_DEV_xxx).
1858  * @param sgl_len Number of elements in the scatter-gather list.
1859  * @param sgl Scatter-gather list describing the memory from which data will be
1860  *        read.
1861  * @param offset Driver-dependent offset.  For a random-access device, this is
1862  *        often a byte offset from the beginning of the device; in other cases,
1863  *        like on a control device, it may have a different meaning.
1864  * @param intarg Value which will be delivered as the intarg member of the
1865  *        eventual interrupt message; the intdata member will be set to the
1866  *        normal return value from the write request.
1867  * @return Zero if the write was successfully scheduled; otherwise, a negative
1868  *         error code.  Note that some drivers may choose to pre-validate
1869  *         their arguments, and may thus detect certain device error
1870  *         conditions at this time rather than when the completion notification
1871  *         occurs, but this is not required.
1872  */
1873 int hv_dev_pwritea(int devhdl, __hv32 flags, __hv32 sgl_len,
1874                    HV_SGL sgl[/* sgl_len */], __hv64 offset, HV_IntArg intarg);
1875
1876
1877 /** Define a pair of tile and ASID to identify a user process context. */
1878 typedef struct
1879 {
1880   /** X coordinate, relative to supervisor's top-left coordinate */
1881   unsigned int x:11;
1882
1883   /** Y coordinate, relative to supervisor's top-left coordinate */
1884   unsigned int y:11;
1885
1886   /** ASID of the process on this x,y tile */
1887   HV_ASID asid:10;
1888 } HV_Remote_ASID;
1889
1890 /** Flush cache and/or TLB state on remote tiles.
1891  *
1892  * @param cache_pa Client physical address to flush from cache (ignored if
1893  *        the length encoded in cache_control is zero, or if
1894  *        HV_FLUSH_EVICT_L2 is set, or if cache_cpumask is NULL).
1895  * @param cache_control This argument allows you to specify a length of
1896  *        physical address space to flush (maximum HV_FLUSH_MAX_CACHE_LEN).
1897  *        You can "or" in HV_FLUSH_EVICT_L2 to flush the whole L2 cache.
1898  *        You can "or" in HV_FLUSH_EVICT_L1I to flush the whole L1I cache.
1899  *        HV_FLUSH_ALL flushes all caches.
1900  * @param cache_cpumask Bitmask (in row-major order, supervisor-relative) of
1901  *        tile indices to perform cache flush on.  The low bit of the first
1902  *        word corresponds to the tile at the upper left-hand corner of the
1903  *        supervisor's rectangle.  If passed as a NULL pointer, equivalent
1904  *        to an empty bitmask.  On chips which support hash-for-home caching,
1905  *        if passed as -1, equivalent to a mask containing tiles which could
1906  *        be doing hash-for-home caching.
1907  * @param tlb_va Virtual address to flush from TLB (ignored if
1908  *        tlb_length is zero or tlb_cpumask is NULL).
1909  * @param tlb_length Number of bytes of data to flush from the TLB.
1910  * @param tlb_pgsize Page size to use for TLB flushes.
1911  *        tlb_va and tlb_length need not be aligned to this size.
1912  * @param tlb_cpumask Bitmask for tlb flush, like cache_cpumask.
1913  *        If passed as a NULL pointer, equivalent to an empty bitmask.
1914  * @param asids Pointer to an HV_Remote_ASID array of tile/ASID pairs to flush.
1915  * @param asidcount Number of HV_Remote_ASID entries in asids[].
1916  * @return Zero for success, or else HV_EINVAL or HV_EFAULT for errors that
1917  *        are detected while parsing the arguments.
1918  */
1919 int hv_flush_remote(HV_PhysAddr cache_pa, unsigned long cache_control,
1920                     unsigned long* cache_cpumask,
1921                     HV_VirtAddr tlb_va, unsigned long tlb_length,
1922                     unsigned long tlb_pgsize, unsigned long* tlb_cpumask,
1923                     HV_Remote_ASID* asids, int asidcount);
1924
1925 /** Include in cache_control to ensure a flush of the entire L2. */
1926 #define HV_FLUSH_EVICT_L2 (1UL << 31)
1927
1928 /** Include in cache_control to ensure a flush of the entire L1I. */
1929 #define HV_FLUSH_EVICT_L1I (1UL << 30)
1930
1931 /** Maximum legal size to use for the "length" component of cache_control. */
1932 #define HV_FLUSH_MAX_CACHE_LEN ((1UL << 30) - 1)
1933
1934 /** Use for cache_control to ensure a flush of all caches. */
1935 #define HV_FLUSH_ALL -1UL
1936
1937 #else   /* __ASSEMBLER__ */
1938
1939 /** Include in cache_control to ensure a flush of the entire L2. */
1940 #define HV_FLUSH_EVICT_L2 (1 << 31)
1941
1942 /** Include in cache_control to ensure a flush of the entire L1I. */
1943 #define HV_FLUSH_EVICT_L1I (1 << 30)
1944
1945 /** Maximum legal size to use for the "length" component of cache_control. */
1946 #define HV_FLUSH_MAX_CACHE_LEN ((1 << 30) - 1)
1947
1948 /** Use for cache_control to ensure a flush of all caches. */
1949 #define HV_FLUSH_ALL -1
1950
1951 #endif  /* __ASSEMBLER__ */
1952
1953 #ifndef __ASSEMBLER__
1954
1955 /** Return a 64-bit value corresponding to the PTE if needed */
1956 #define hv_pte_val(pte) ((pte).val)
1957
1958 /** Cast a 64-bit value to an HV_PTE */
1959 #define hv_pte(val) ((HV_PTE) { val })
1960
1961 #endif  /* !__ASSEMBLER__ */
1962
1963
1964 /** Bits in the size of an HV_PTE */
1965 #define HV_LOG2_PTE_SIZE 3
1966
1967 /** Size of an HV_PTE */
1968 #define HV_PTE_SIZE (1 << HV_LOG2_PTE_SIZE)
1969
1970
1971 /* Bits in HV_PTE's low word. */
1972 #define HV_PTE_INDEX_PRESENT          0  /**< PTE is valid */
1973 #define HV_PTE_INDEX_MIGRATING        1  /**< Page is migrating */
1974 #define HV_PTE_INDEX_CLIENT0          2  /**< Page client state 0 */
1975 #define HV_PTE_INDEX_CLIENT1          3  /**< Page client state 1 */
1976 #define HV_PTE_INDEX_NC               4  /**< L1$/L2$ incoherent with L3$ */
1977 #define HV_PTE_INDEX_NO_ALLOC_L1      5  /**< Page is uncached in local L1$ */
1978 #define HV_PTE_INDEX_NO_ALLOC_L2      6  /**< Page is uncached in local L2$ */
1979 #define HV_PTE_INDEX_CACHED_PRIORITY  7  /**< Page is priority cached */
1980 #define HV_PTE_INDEX_PAGE             8  /**< PTE describes a page */
1981 #define HV_PTE_INDEX_GLOBAL           9  /**< Page is global */
1982 #define HV_PTE_INDEX_USER            10  /**< Page is user-accessible */
1983 #define HV_PTE_INDEX_ACCESSED        11  /**< Page has been accessed */
1984 #define HV_PTE_INDEX_DIRTY           12  /**< Page has been written */
1985                                          /*   Bits 13-14 are reserved for
1986                                               future use. */
1987 #define HV_PTE_INDEX_SUPER           15  /**< Pages ganged together for TLB */
1988 #define HV_PTE_INDEX_MODE            16  /**< Page mode; see HV_PTE_MODE_xxx */
1989 #define HV_PTE_MODE_BITS              3  /**< Number of bits in mode */
1990 #define HV_PTE_INDEX_CLIENT2         19  /**< Page client state 2 */
1991 #define HV_PTE_INDEX_LOTAR           20  /**< Page's LOTAR; must be high bits
1992                                               of word */
1993 #define HV_PTE_LOTAR_BITS            12  /**< Number of bits in a LOTAR */
1994
1995 /* Bits in HV_PTE's high word. */
1996 #define HV_PTE_INDEX_READABLE        32  /**< Page is readable */
1997 #define HV_PTE_INDEX_WRITABLE        33  /**< Page is writable */
1998 #define HV_PTE_INDEX_EXECUTABLE      34  /**< Page is executable */
1999 #define HV_PTE_INDEX_PTFN            35  /**< Page's PTFN; must be high bits
2000                                               of word */
2001 #define HV_PTE_PTFN_BITS             29  /**< Number of bits in a PTFN */
2002
2003 /*
2004  * Legal values for the PTE's mode field
2005  */
2006 /** Data is not resident in any caches; loads and stores access memory
2007  *  directly.
2008  */
2009 #define HV_PTE_MODE_UNCACHED          1
2010
2011 /** Data is resident in the tile's local L1 and/or L2 caches; if a load
2012  *  or store misses there, it goes to memory.
2013  *
2014  *  The copy in the local L1$/L2$ is not invalidated when the copy in
2015  *  memory is changed.
2016  */
2017 #define HV_PTE_MODE_CACHE_NO_L3       2
2018
2019 /** Data is resident in the tile's local L1 and/or L2 caches.  If a load
2020  *  or store misses there, it goes to an L3 cache in a designated tile;
2021  *  if it misses there, it goes to memory.
2022  *
2023  *  If the NC bit is not set, the copy in the local L1$/L2$ is invalidated
2024  *  when the copy in the remote L3$ is changed.  Otherwise, such
2025  *  invalidation will not occur.
2026  *
2027  *  Chips for which CHIP_HAS_COHERENT_LOCAL_CACHE() is 0 do not support
2028  *  invalidation from an L3$ to another tile's L1$/L2$.  If the NC bit is
2029  *  clear on such a chip, no copy is kept in the local L1$/L2$ in this mode.
2030  */
2031 #define HV_PTE_MODE_CACHE_TILE_L3     3
2032
2033 /** Data is resident in the tile's local L1 and/or L2 caches.  If a load
2034  *  or store misses there, it goes to an L3 cache in one of a set of
2035  *  designated tiles; if it misses there, it goes to memory.  Which tile
2036  *  is chosen from the set depends upon a hash function applied to the
2037  *  physical address.  This mode is not supported on chips for which
2038  *  CHIP_HAS_CBOX_HOME_MAP() is 0.
2039  *
2040  *  If the NC bit is not set, the copy in the local L1$/L2$ is invalidated
2041  *  when the copy in the remote L3$ is changed.  Otherwise, such
2042  *  invalidation will not occur.
2043  *
2044  *  Chips for which CHIP_HAS_COHERENT_LOCAL_CACHE() is 0 do not support
2045  *  invalidation from an L3$ to another tile's L1$/L2$.  If the NC bit is
2046  *  clear on such a chip, no copy is kept in the local L1$/L2$ in this mode.
2047  */
2048 #define HV_PTE_MODE_CACHE_HASH_L3     4
2049
2050 /** Data is not resident in memory; accesses are instead made to an I/O
2051  *  device, whose tile coordinates are given by the PTE's LOTAR field.
2052  *  This mode is only supported on chips for which CHIP_HAS_MMIO() is 1.
2053  *  The EXECUTABLE bit may not be set in an MMIO PTE.
2054  */
2055 #define HV_PTE_MODE_MMIO              5
2056
2057
2058 /* C wants 1ULL so it is typed as __hv64, but the assembler needs just numbers.
2059  * The assembler can't handle shifts greater than 31, but treats them
2060  * as shifts mod 32, so assembler code must be aware of which word
2061  * the bit belongs in when using these macros.
2062  */
2063 #ifdef __ASSEMBLER__
2064 #define __HV_PTE_ONE 1        /**< One, for assembler */
2065 #else
2066 #define __HV_PTE_ONE 1ULL     /**< One, for C */
2067 #endif
2068
2069 /** Is this PTE present?
2070  *
2071  * If this bit is set, this PTE represents a valid translation or level-2
2072  * page table pointer.  Otherwise, the page table does not contain a
2073  * translation for the subject virtual pages.
2074  *
2075  * If this bit is not set, the other bits in the PTE are not
2076  * interpreted by the hypervisor, and may contain any value.
2077  */
2078 #define HV_PTE_PRESENT               (__HV_PTE_ONE << HV_PTE_INDEX_PRESENT)
2079
2080 /** Does this PTE map a page?
2081  *
2082  * If this bit is set in a level-0 page table, the entry should be
2083  * interpreted as a level-2 page table entry mapping a jumbo page.
2084  *
2085  * If this bit is set in a level-1 page table, the entry should be
2086  * interpreted as a level-2 page table entry mapping a large page.
2087  *
2088  * This bit should not be modified by the client while PRESENT is set, as
2089  * doing so may race with the hypervisor's update of ACCESSED and DIRTY bits.
2090  *
2091  * In a level-2 page table, this bit is ignored and must be zero.
2092  */
2093 #define HV_PTE_PAGE                  (__HV_PTE_ONE << HV_PTE_INDEX_PAGE)
2094
2095 /** Does this PTE implicitly reference multiple pages?
2096  *
2097  * If this bit is set in the page table (either in the level-2 page table,
2098  * or in a higher level page table in conjunction with the PAGE bit)
2099  * then the PTE specifies a range of contiguous pages, not a single page.
2100  * The hv_set_pte_super_shift() allows you to specify the count for
2101  * each level of the page table.
2102  *
2103  * Note: this bit is not supported on TILEPro systems.
2104  */
2105 #define HV_PTE_SUPER                 (__HV_PTE_ONE << HV_PTE_INDEX_SUPER)
2106
2107 /** Is this a global (non-ASID) mapping?
2108  *
2109  * If this bit is set, the translations established by this PTE will
2110  * not be flushed from the TLB by the hv_flush_asid() service; they
2111  * will be flushed by the hv_flush_page() or hv_flush_pages() services.
2112  *
2113  * Setting this bit for translations which are identical in all page
2114  * tables (for instance, code and data belonging to a client OS) can
2115  * be very beneficial, as it will reduce the number of TLB misses.
2116  * Note that, while it is not an error which will be detected by the
2117  * hypervisor, it is an extremely bad idea to set this bit for
2118  * translations which are _not_ identical in all page tables.
2119  *
2120  * This bit should not be modified by the client while PRESENT is set, as
2121  * doing so may race with the hypervisor's update of ACCESSED and DIRTY bits.
2122  *
2123  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2124  */
2125 #define HV_PTE_GLOBAL                (__HV_PTE_ONE << HV_PTE_INDEX_GLOBAL)
2126
2127 /** Is this mapping accessible to users?
2128  *
2129  * If this bit is set, code running at any PL will be permitted to
2130  * access the virtual addresses mapped by this PTE.  Otherwise, only
2131  * code running at PL 1 or above will be allowed to do so.
2132  *
2133  * This bit should not be modified by the client while PRESENT is set, as
2134  * doing so may race with the hypervisor's update of ACCESSED and DIRTY bits.
2135  *
2136  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2137  */
2138 #define HV_PTE_USER                  (__HV_PTE_ONE << HV_PTE_INDEX_USER)
2139
2140 /** Has this mapping been accessed?
2141  *
2142  * This bit is set by the hypervisor when the memory described by the
2143  * translation is accessed for the first time.  It is never cleared by
2144  * the hypervisor, but may be cleared by the client.  After the bit
2145  * has been cleared, subsequent references are not guaranteed to set
2146  * it again until the translation has been flushed from the TLB.
2147  *
2148  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2149  */
2150 #define HV_PTE_ACCESSED              (__HV_PTE_ONE << HV_PTE_INDEX_ACCESSED)
2151
2152 /** Is this mapping dirty?
2153  *
2154  * This bit is set by the hypervisor when the memory described by the
2155  * translation is written for the first time.  It is never cleared by
2156  * the hypervisor, but may be cleared by the client.  After the bit
2157  * has been cleared, subsequent references are not guaranteed to set
2158  * it again until the translation has been flushed from the TLB.
2159  *
2160  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2161  */
2162 #define HV_PTE_DIRTY                 (__HV_PTE_ONE << HV_PTE_INDEX_DIRTY)
2163
2164 /** Migrating bit in PTE.
2165  *
2166  * This bit is guaranteed not to be inspected or modified by the
2167  * hypervisor.  The name is indicative of the suggested use by the client
2168  * to tag pages whose L3 cache is being migrated from one cpu to another.
2169  */
2170 #define HV_PTE_MIGRATING             (__HV_PTE_ONE << HV_PTE_INDEX_MIGRATING)
2171
2172 /** Client-private bit in PTE.
2173  *
2174  * This bit is guaranteed not to be inspected or modified by the
2175  * hypervisor.
2176  */
2177 #define HV_PTE_CLIENT0               (__HV_PTE_ONE << HV_PTE_INDEX_CLIENT0)
2178
2179 /** Client-private bit in PTE.
2180  *
2181  * This bit is guaranteed not to be inspected or modified by the
2182  * hypervisor.
2183  */
2184 #define HV_PTE_CLIENT1               (__HV_PTE_ONE << HV_PTE_INDEX_CLIENT1)
2185
2186 /** Client-private bit in PTE.
2187  *
2188  * This bit is guaranteed not to be inspected or modified by the
2189  * hypervisor.
2190  */
2191 #define HV_PTE_CLIENT2               (__HV_PTE_ONE << HV_PTE_INDEX_CLIENT2)
2192
2193 /** Non-coherent (NC) bit in PTE.
2194  *
2195  * If this bit is set, the mapping that is set up will be non-coherent
2196  * (also known as non-inclusive).  This means that changes to the L3
2197  * cache will not cause a local copy to be invalidated.  It is generally
2198  * recommended only for read-only mappings.
2199  *
2200  * In level-1 PTEs, if the Page bit is clear, this bit determines how the
2201  * level-2 page table is accessed.
2202  */
2203 #define HV_PTE_NC                    (__HV_PTE_ONE << HV_PTE_INDEX_NC)
2204
2205 /** Is this page prevented from filling the L1$?
2206  *
2207  * If this bit is set, the page described by the PTE will not be cached
2208  * the local cpu's L1 cache.
2209  *
2210  * If CHIP_HAS_NC_AND_NOALLOC_BITS() is not true in <chip.h> for this chip,
2211  * it is illegal to use this attribute, and may cause client termination.
2212  *
2213  * In level-1 PTEs, if the Page bit is clear, this bit
2214  * determines how the level-2 page table is accessed.
2215  */
2216 #define HV_PTE_NO_ALLOC_L1           (__HV_PTE_ONE << HV_PTE_INDEX_NO_ALLOC_L1)
2217
2218 /** Is this page prevented from filling the L2$?
2219  *
2220  * If this bit is set, the page described by the PTE will not be cached
2221  * the local cpu's L2 cache.
2222  *
2223  * If CHIP_HAS_NC_AND_NOALLOC_BITS() is not true in <chip.h> for this chip,
2224  * it is illegal to use this attribute, and may cause client termination.
2225  *
2226  * In level-1 PTEs, if the Page bit is clear, this bit determines how the
2227  * level-2 page table is accessed.
2228  */
2229 #define HV_PTE_NO_ALLOC_L2           (__HV_PTE_ONE << HV_PTE_INDEX_NO_ALLOC_L2)
2230
2231 /** Is this a priority page?
2232  *
2233  * If this bit is set, the page described by the PTE will be given
2234  * priority in the cache.  Normally this translates into allowing the
2235  * page to use only the "red" half of the cache.  The client may wish to
2236  * then use the hv_set_caching service to specify that other pages which
2237  * alias this page will use only the "black" half of the cache.
2238  *
2239  * If the Cached Priority bit is clear, the hypervisor uses the
2240  * current hv_set_caching() value to choose how to cache the page.
2241  *
2242  * It is illegal to set the Cached Priority bit if the Non-Cached bit
2243  * is set and the Cached Remotely bit is clear, i.e. if requests to
2244  * the page map directly to memory.
2245  *
2246  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2247  */
2248 #define HV_PTE_CACHED_PRIORITY       (__HV_PTE_ONE << \
2249                                       HV_PTE_INDEX_CACHED_PRIORITY)
2250
2251 /** Is this a readable mapping?
2252  *
2253  * If this bit is set, code will be permitted to read from (e.g.,
2254  * issue load instructions against) the virtual addresses mapped by
2255  * this PTE.
2256  *
2257  * It is illegal for this bit to be clear if the Writable bit is set.
2258  *
2259  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2260  */
2261 #define HV_PTE_READABLE              (__HV_PTE_ONE << HV_PTE_INDEX_READABLE)
2262
2263 /** Is this a writable mapping?
2264  *
2265  * If this bit is set, code will be permitted to write to (e.g., issue
2266  * store instructions against) the virtual addresses mapped by this
2267  * PTE.
2268  *
2269  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2270  */
2271 #define HV_PTE_WRITABLE              (__HV_PTE_ONE << HV_PTE_INDEX_WRITABLE)
2272
2273 /** Is this an executable mapping?
2274  *
2275  * If this bit is set, code will be permitted to execute from
2276  * (e.g., jump to) the virtual addresses mapped by this PTE.
2277  *
2278  * This bit applies to any processor on the tile, if there are more
2279  * than one.
2280  *
2281  * This bit is ignored in level-1 PTEs unless the Page bit is set.
2282  */
2283 #define HV_PTE_EXECUTABLE            (__HV_PTE_ONE << HV_PTE_INDEX_EXECUTABLE)
2284
2285 /** The width of a LOTAR's x or y bitfield. */
2286 #define HV_LOTAR_WIDTH 11
2287
2288 /** Converts an x,y pair to a LOTAR value. */
2289 #define HV_XY_TO_LOTAR(x, y) ((HV_LOTAR)(((x) << HV_LOTAR_WIDTH) | (y)))
2290
2291 /** Extracts the X component of a lotar. */
2292 #define HV_LOTAR_X(lotar) ((lotar) >> HV_LOTAR_WIDTH)
2293
2294 /** Extracts the Y component of a lotar. */
2295 #define HV_LOTAR_Y(lotar) ((lotar) & ((1 << HV_LOTAR_WIDTH) - 1))
2296
2297 #ifndef __ASSEMBLER__
2298
2299 /** Define accessor functions for a PTE bit. */
2300 #define _HV_BIT(name, bit)                                      \
2301 static __inline int                                             \
2302 hv_pte_get_##name(HV_PTE pte)                                   \
2303 {                                                               \
2304   return (pte.val >> HV_PTE_INDEX_##bit) & 1;                   \
2305 }                                                               \
2306                                                                 \
2307 static __inline HV_PTE                                          \
2308 hv_pte_set_##name(HV_PTE pte)                                   \
2309 {                                                               \
2310   pte.val |= 1ULL << HV_PTE_INDEX_##bit;                        \
2311   return pte;                                                   \
2312 }                                                               \
2313                                                                 \
2314 static __inline HV_PTE                                          \
2315 hv_pte_clear_##name(HV_PTE pte)                                 \
2316 {                                                               \
2317   pte.val &= ~(1ULL << HV_PTE_INDEX_##bit);                     \
2318   return pte;                                                   \
2319 }
2320
2321 /* Generate accessors to get, set, and clear various PTE flags.
2322  */
2323 _HV_BIT(present,         PRESENT)
2324 _HV_BIT(page,            PAGE)
2325 _HV_BIT(super,           SUPER)
2326 _HV_BIT(client0,         CLIENT0)
2327 _HV_BIT(client1,         CLIENT1)
2328 _HV_BIT(client2,         CLIENT2)
2329 _HV_BIT(migrating,       MIGRATING)
2330 _HV_BIT(nc,              NC)
2331 _HV_BIT(readable,        READABLE)
2332 _HV_BIT(writable,        WRITABLE)
2333 _HV_BIT(executable,      EXECUTABLE)
2334 _HV_BIT(accessed,        ACCESSED)
2335 _HV_BIT(dirty,           DIRTY)
2336 _HV_BIT(no_alloc_l1,     NO_ALLOC_L1)
2337 _HV_BIT(no_alloc_l2,     NO_ALLOC_L2)
2338 _HV_BIT(cached_priority, CACHED_PRIORITY)
2339 _HV_BIT(global,          GLOBAL)
2340 _HV_BIT(user,            USER)
2341
2342 #undef _HV_BIT
2343
2344 /** Get the page mode from the PTE.
2345  *
2346  * This field generally determines whether and how accesses to the page
2347  * are cached; the HV_PTE_MODE_xxx symbols define the legal values for the
2348  * page mode.  The NC, NO_ALLOC_L1, and NO_ALLOC_L2 bits modify this
2349  * general policy.
2350  */
2351 static __inline unsigned int
2352 hv_pte_get_mode(const HV_PTE pte)
2353 {
2354   return (((__hv32) pte.val) >> HV_PTE_INDEX_MODE) &
2355          ((1 << HV_PTE_MODE_BITS) - 1);
2356 }
2357
2358 /** Set the page mode into a PTE.  See hv_pte_get_mode. */
2359 static __inline HV_PTE
2360 hv_pte_set_mode(HV_PTE pte, unsigned int val)
2361 {
2362   pte.val &= ~(((1ULL << HV_PTE_MODE_BITS) - 1) << HV_PTE_INDEX_MODE);
2363   pte.val |= val << HV_PTE_INDEX_MODE;
2364   return pte;
2365 }
2366
2367 /** Get the page frame number from the PTE.
2368  *
2369  * This field contains the upper bits of the CPA (client physical
2370  * address) of the target page; the complete CPA is this field with
2371  * HV_LOG2_PAGE_TABLE_ALIGN zero bits appended to it.
2372  *
2373  * For all PTEs in the lowest-level page table, and for all PTEs with
2374  * the Page bit set in all page tables, the CPA must be aligned modulo
2375  * the relevant page size.
2376  */
2377 static __inline unsigned long
2378 hv_pte_get_ptfn(const HV_PTE pte)
2379 {
2380   return pte.val >> HV_PTE_INDEX_PTFN;
2381 }
2382
2383 /** Set the page table frame number into a PTE.  See hv_pte_get_ptfn. */
2384 static __inline HV_PTE
2385 hv_pte_set_ptfn(HV_PTE pte, unsigned long val)
2386 {
2387   pte.val &= ~(((1ULL << HV_PTE_PTFN_BITS)-1) << HV_PTE_INDEX_PTFN);
2388   pte.val |= (__hv64) val << HV_PTE_INDEX_PTFN;
2389   return pte;
2390 }
2391
2392 /** Get the client physical address from the PTE.  See hv_pte_set_ptfn. */
2393 static __inline HV_PhysAddr
2394 hv_pte_get_pa(const HV_PTE pte)
2395 {
2396   return (__hv64) hv_pte_get_ptfn(pte) << HV_LOG2_PAGE_TABLE_ALIGN;
2397 }
2398
2399 /** Set the client physical address into a PTE.  See hv_pte_get_ptfn. */
2400 static __inline HV_PTE
2401 hv_pte_set_pa(HV_PTE pte, HV_PhysAddr pa)
2402 {
2403   return hv_pte_set_ptfn(pte, pa >> HV_LOG2_PAGE_TABLE_ALIGN);
2404 }
2405
2406
2407 /** Get the remote tile caching this page.
2408  *
2409  * Specifies the remote tile which is providing the L3 cache for this page.
2410  *
2411  * This field is ignored unless the page mode is HV_PTE_MODE_CACHE_TILE_L3.
2412  *
2413  * In level-1 PTEs, if the Page bit is clear, this field determines how the
2414  * level-2 page table is accessed.
2415  */
2416 static __inline unsigned int
2417 hv_pte_get_lotar(const HV_PTE pte)
2418 {
2419   unsigned int lotar = ((__hv32) pte.val) >> HV_PTE_INDEX_LOTAR;
2420
2421   return HV_XY_TO_LOTAR( (lotar >> (HV_PTE_LOTAR_BITS / 2)),
2422                          (lotar & ((1 << (HV_PTE_LOTAR_BITS / 2)) - 1)) );
2423 }
2424
2425
2426 /** Set the remote tile caching a page into a PTE.  See hv_pte_get_lotar. */
2427 static __inline HV_PTE
2428 hv_pte_set_lotar(HV_PTE pte, unsigned int val)
2429 {
2430   unsigned int x = HV_LOTAR_X(val);
2431   unsigned int y = HV_LOTAR_Y(val);
2432
2433   pte.val &= ~(((1ULL << HV_PTE_LOTAR_BITS)-1) << HV_PTE_INDEX_LOTAR);
2434   pte.val |= (x << (HV_PTE_INDEX_LOTAR + HV_PTE_LOTAR_BITS / 2)) |
2435              (y << HV_PTE_INDEX_LOTAR);
2436   return pte;
2437 }
2438
2439 #endif  /* !__ASSEMBLER__ */
2440
2441 /** Converts a client physical address to a ptfn. */
2442 #define HV_CPA_TO_PTFN(p) ((p) >> HV_LOG2_PAGE_TABLE_ALIGN)
2443
2444 /** Converts a ptfn to a client physical address. */
2445 #define HV_PTFN_TO_CPA(p) (((HV_PhysAddr)(p)) << HV_LOG2_PAGE_TABLE_ALIGN)
2446
2447 #if CHIP_VA_WIDTH() > 32
2448
2449 /*
2450  * Note that we currently do not allow customizing the page size
2451  * of the L0 pages, but fix them at 4GB, so we do not use the
2452  * "_HV_xxx" nomenclature for the L0 macros.
2453  */
2454
2455 /** Log number of HV_PTE entries in L0 page table */
2456 #define HV_LOG2_L0_ENTRIES (CHIP_VA_WIDTH() - HV_LOG2_L1_SPAN)
2457
2458 /** Number of HV_PTE entries in L0 page table */
2459 #define HV_L0_ENTRIES (1 << HV_LOG2_L0_ENTRIES)
2460
2461 /** Log size of L0 page table in bytes */
2462 #define HV_LOG2_L0_SIZE (HV_LOG2_PTE_SIZE + HV_LOG2_L0_ENTRIES)
2463
2464 /** Size of L0 page table in bytes */
2465 #define HV_L0_SIZE (1 << HV_LOG2_L0_SIZE)
2466
2467 #ifdef __ASSEMBLER__
2468
2469 /** Index in L0 for a specific VA */
2470 #define HV_L0_INDEX(va) \
2471   (((va) >> HV_LOG2_L1_SPAN) & (HV_L0_ENTRIES - 1))
2472
2473 #else
2474
2475 /** Index in L1 for a specific VA */
2476 #define HV_L0_INDEX(va) \
2477   (((HV_VirtAddr)(va) >> HV_LOG2_L1_SPAN) & (HV_L0_ENTRIES - 1))
2478
2479 #endif
2480
2481 #endif /* CHIP_VA_WIDTH() > 32 */
2482
2483 /** Log number of HV_PTE entries in L1 page table */
2484 #define _HV_LOG2_L1_ENTRIES(log2_page_size_large) \
2485   (HV_LOG2_L1_SPAN - log2_page_size_large)
2486
2487 /** Number of HV_PTE entries in L1 page table */
2488 #define _HV_L1_ENTRIES(log2_page_size_large) \
2489   (1 << _HV_LOG2_L1_ENTRIES(log2_page_size_large))
2490
2491 /** Log size of L1 page table in bytes */
2492 #define _HV_LOG2_L1_SIZE(log2_page_size_large) \
2493   (HV_LOG2_PTE_SIZE + _HV_LOG2_L1_ENTRIES(log2_page_size_large))
2494
2495 /** Size of L1 page table in bytes */
2496 #define _HV_L1_SIZE(log2_page_size_large) \
2497   (1 << _HV_LOG2_L1_SIZE(log2_page_size_large))
2498
2499 /** Log number of HV_PTE entries in level-2 page table */
2500 #define _HV_LOG2_L2_ENTRIES(log2_page_size_large, log2_page_size_small) \
2501   (log2_page_size_large - log2_page_size_small)
2502
2503 /** Number of HV_PTE entries in level-2 page table */
2504 #define _HV_L2_ENTRIES(log2_page_size_large, log2_page_size_small) \
2505   (1 << _HV_LOG2_L2_ENTRIES(log2_page_size_large, log2_page_size_small))
2506
2507 /** Log size of level-2 page table in bytes */
2508 #define _HV_LOG2_L2_SIZE(log2_page_size_large, log2_page_size_small) \
2509   (HV_LOG2_PTE_SIZE + \
2510    _HV_LOG2_L2_ENTRIES(log2_page_size_large, log2_page_size_small))
2511
2512 /** Size of level-2 page table in bytes */
2513 #define _HV_L2_SIZE(log2_page_size_large, log2_page_size_small) \
2514   (1 << _HV_LOG2_L2_SIZE(log2_page_size_large, log2_page_size_small))
2515
2516 #ifdef __ASSEMBLER__
2517
2518 #if CHIP_VA_WIDTH() > 32
2519
2520 /** Index in L1 for a specific VA */
2521 #define _HV_L1_INDEX(va, log2_page_size_large) \
2522   (((va) >> log2_page_size_large) & (_HV_L1_ENTRIES(log2_page_size_large) - 1))
2523
2524 #else /* CHIP_VA_WIDTH() > 32 */
2525
2526 /** Index in L1 for a specific VA */
2527 #define _HV_L1_INDEX(va, log2_page_size_large) \
2528   (((va) >> log2_page_size_large))
2529
2530 #endif /* CHIP_VA_WIDTH() > 32 */
2531
2532 /** Index in level-2 page table for a specific VA */
2533 #define _HV_L2_INDEX(va, log2_page_size_large, log2_page_size_small) \
2534   (((va) >> log2_page_size_small) & \
2535    (_HV_L2_ENTRIES(log2_page_size_large, log2_page_size_small) - 1))
2536
2537 #else /* __ASSEMBLER __ */
2538
2539 #if CHIP_VA_WIDTH() > 32
2540
2541 /** Index in L1 for a specific VA */
2542 #define _HV_L1_INDEX(va, log2_page_size_large) \
2543   (((HV_VirtAddr)(va) >> log2_page_size_large) & \
2544    (_HV_L1_ENTRIES(log2_page_size_large) - 1))
2545
2546 #else /* CHIP_VA_WIDTH() > 32 */
2547
2548 /** Index in L1 for a specific VA */
2549 #define _HV_L1_INDEX(va, log2_page_size_large) \
2550   (((HV_VirtAddr)(va) >> log2_page_size_large))
2551
2552 #endif /* CHIP_VA_WIDTH() > 32 */
2553
2554 /** Index in level-2 page table for a specific VA */
2555 #define _HV_L2_INDEX(va, log2_page_size_large, log2_page_size_small) \
2556   (((HV_VirtAddr)(va) >> log2_page_size_small) & \
2557    (_HV_L2_ENTRIES(log2_page_size_large, log2_page_size_small) - 1))
2558
2559 #endif /* __ASSEMBLER __ */
2560
2561 /** Position of the PFN field within the PTE (subset of the PTFN). */
2562 #define _HV_PTE_INDEX_PFN(log2_page_size) \
2563   (HV_PTE_INDEX_PTFN + (log2_page_size - HV_LOG2_PAGE_TABLE_ALIGN))
2564
2565 /** Length of the PFN field within the PTE (subset of the PTFN). */
2566 #define _HV_PTE_INDEX_PFN_BITS(log2_page_size) \
2567   (HV_PTE_INDEX_PTFN_BITS - (log2_page_size - HV_LOG2_PAGE_TABLE_ALIGN))
2568
2569 /** Converts a client physical address to a pfn. */
2570 #define _HV_CPA_TO_PFN(p, log2_page_size) ((p) >> log2_page_size)
2571
2572 /** Converts a pfn to a client physical address. */
2573 #define _HV_PFN_TO_CPA(p, log2_page_size) \
2574   (((HV_PhysAddr)(p)) << log2_page_size)
2575
2576 /** Converts a ptfn to a pfn. */
2577 #define _HV_PTFN_TO_PFN(p, log2_page_size) \
2578   ((p) >> (log2_page_size - HV_LOG2_PAGE_TABLE_ALIGN))
2579
2580 /** Converts a pfn to a ptfn. */
2581 #define _HV_PFN_TO_PTFN(p, log2_page_size) \
2582   ((p) << (log2_page_size - HV_LOG2_PAGE_TABLE_ALIGN))
2583
2584 #endif /* _HV_HV_H */