···275275 * use the TSC value at the transitions to calculate a pretty276276 * good value for the TSC frequencty.277277 */278278+static inline int pit_verify_msb(unsigned char val)279279+{280280+ /* Ignore LSB */281281+ inb(0x42);282282+ return inb(0x42) == val;283283+}284284+278285static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap)279286{280287 int count;281288 u64 tsc = 0;282289283290 for (count = 0; count < 50000; count++) {284284- /* Ignore LSB */285285- inb(0x42);286286- if (inb(0x42) != val)291291+ if (!pit_verify_msb(val))287292 break;288293 tsc = get_cycles();289294 }···341336 * to do that is to just read back the 16-bit counter342337 * once from the PIT.343338 */344344- inb(0x42);345345- inb(0x42);339339+ pit_verify_msb(0);346340347341 if (pit_expect_msb(0xff, &tsc, &d1)) {348342 for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {···352348 * Iterate until the error is less than 500 ppm353349 */354350 delta -= tsc;355355- if (d1+d2 < delta >> 11)356356- goto success;351351+ if (d1+d2 >= delta >> 11)352352+ continue;353353+354354+ /*355355+ * Check the PIT one more time to verify that356356+ * all TSC reads were stable wrt the PIT.357357+ *358358+ * This also guarantees serialization of the359359+ * last cycle read ('d2') in pit_expect_msb.360360+ */361361+ if (!pit_verify_msb(0xfe - i))362362+ break;363363+ goto success;357364 }358365 }359366 printk("Fast TSC calibration failed\n");