1 /* Copyright (C) 2012-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library. If not, see
16 <http://www.gnu.org/licenses/>. */
24 #include <arch/spr_def.h>
27 /* The count of cores horizontally (X dimension) on the chip. */
28 static int chip_width
;
30 /* Read the chip "width" from the /sys filesystem. */
32 initialize_chip_width (void)
37 fd
= __open ("/sys/devices/system/cpu/chip_width", O_RDONLY
);
44 n
= __read (fd
, buf
, sizeof (buf
));
47 for (i
= 0; i
< n
; ++i
)
49 if (buf
[i
] < '0' || buf
[i
] > '9')
51 w
= (w
* 10) + (buf
[i
] - '0');
55 /* Store a negative value so we don't try again. */
59 /* Using an atomic idempotent write here makes this thread-safe. */
70 if (__builtin_expect (w
<= 0, 0))
73 w
= initialize_chip_width ();
77 int r
= INLINE_SYSCALL (getcpu
, 3, &cpu
, NULL
, NULL
);
78 return r
== -1 ? r
: cpu
;
82 /* Assign 64-bit value to a 32-bit variable to ensure 32-bit multiply. */
83 coord
= __insn_mfspr (SPR_TILE_COORD
);
85 /* Extract Y coord from bits 7..10 and X coord from bits 18..21. */
86 return ((coord
>> 7) & 0xf) * w
+ ((coord
>> 18) & 0xf);