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